This topic contains and tries to cover following subjects
- Explanation of CAST () function in SQL
- Syntax of CAST () function in SQL
- Example to CAST () function in SQL
Articles contains and tries to provide answer to following questions and issues:
- What is CAST () function
- How to use CAST () function..
- How to convert one data type to another
Articles may require following knowledge:
- Basic query flow in SQL
- SQL Server Management Studio
- SQL Server Data types
Explanation of CAST () function in SQL
CAST function is used to convert SQL data types to each other. SQL server performs conversion tasks implicitly in normal case. However, we can guarantee the conversion with using cast in case we require.
Syntax of CAST () function in SQL
Syntax for CAST () function is as follows:
CAST ( - source data - AS target type(size) )
Example of CAST () function in SQL
To demonstrate how to use CAST function, we will supply an int value to the function. Our final returned data type will be varchar . We convert int data type into nvarchar .
Query is as follows:
As above query execution result indicates, conversion query is executed and nvarchar type data is returned.