SQL CAST

Wednesday, 28 October 2015

SQL CAST

The CAST () function in the SQL language is a cast function that converts data from one type to another. For example it is possible to transform a date in datetime format DATE, or vice versa.

Syntax:


The syntax of the CAST function is:

SELECT CAST (Expression AS Datatype);

In this syntax, "expression" is the value to be transformed, while "Datatype" contains the type of data that have to be obtained. This type of data can be one of the following (depending on the database management system):

Data Type may be different Like:
--------------------------------
BINARY
CHAR
DATE
DATETIME
TIME
INT etc.
--------------------------------

Example:


DECLARE @First varchar(2)
DECLARE @Second varchar(2)
DECLARE @Third varchar(2)
set @First=25
set @Second=15
set @Third=33

Select CAST(@First as int) + CAST(@Second as int) +CAST (@Third as int) as Result

Output:73

No comments:

Post a comment