In this post explains how to use the CAST function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the CAST function converts an expression from one datatype to another datatype. If the conversion fails, the function will return an error. Otherwise, it will return the converted value.
The syntax for the CAST function in SQL Server (Transact-SQL) is:
The value to convert to another datatype.
The datatype that you wish to convert expression to. It can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image.
Optional. The length of the resulting data type for char, varchar, nchar, nvarchar, binary and varbinary.
The CAST function can be used in the following versions of SQL Server (Transact-SQL):
Let's look at some SQL Server CAST function examples and explore how to use the CAST function in SQL Server (Transact-SQL).
For example: