Change the data type of a value to integer (int).
SELECT CAST(25.65 AS int); |
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.
CAST(expression AS datatype(length)) |
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. |
Works in: |
SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse |
Change the data type of a value to varchar.
SELECT CAST(25.65 AS varchar); |
Change the data type of a value to datetime.
SELECT CAST(‘2017-08-25’ AS datetime); |