What is a cast function in database?

By Arsal Ahmed   Posted on March-20-2018 

What is a cast function in database?

In the SQL Server, the cast function converts an expression from one datatype to another datatype.

Expression:

  • The value to convert to another datatype.

Type:

  • The datatype that you wish to convert expression to and it can be one of the following are:

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. The length of the resulting data type for char, varchar, nchar, nvarchar, binary and varbinary.

Example:

  • 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:

SELECT CAST (14.85 AS int);

Result: 14 (result is truncated)

In the SQL Server, the cast function converts an expression from one datatype to another datatype.

Expression:

  • The value to convert to another datatype.

Type:

  • The datatype that you wish to convert expression to and it can be one of the following are:

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. The length of the resulting data type for char, varchar, nchar, nvarchar, binary and varbinary.

Example:

  • 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:

SELECT CAST (14.85 AS int);

Result: 14 (result is truncated)

By Arsal Ahmed    20-Mar-2018 



You may also read following recent articles