Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

CAST

Example

Change the data type of a value to integer (int).

SELECT CAST(25.65 AS int);

Definition and Usage

The CAST() function transforms a value of any type into a designated data type, offering flexibility in data handling within SQL queries.

Tip: Consider using the CONVERT() function for additional formatting capabilities.

Syntax

CAST(expression AS datatype(length))

Parameter Values

Parameter

Description

expression

Mandatory. The value to be converted to a different data type.

datatype

Mandatory. Specifies the target data type to which the expression will be converted. Options include: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image.

(length)

Optional. Specifies the length or size of the resulting data type, applicable to char, varchar, nchar, nvarchar, binary, and varbinary conversions.

Technical Details

Works in:

 SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

More Examples

Example

Change the data type of a value to varchar.

SELECT CAST(25.65 AS varchar);

Example

Change the data type of a value to datetime.

SELECT CAST(‘2017-08-25’ AS datetime);