T-SQL Tutorial

T-SQL CAST


The CAST function convert an expression of one data type to another.

CAST Syntax


CAST ( expression AS datatype [ ( length ) ] )



CAST Example


SELECT CAST('22211' AS INT);

SELECT CAST(CAST ('222.58' AS NUMERIC) AS INT);
SELECT CAST(CAST ('222.6789' AS NUMERIC(19,4)) AS INT);

DECLARE @var decimal (9, 2);
SET @var = 1278.69;
SELECT CAST(@var AS INT);
SELECT CAST(@var AS VARCHAR(25));

See also:
T-SQL Functions
Convert -> Parse -> Try_Cast -> Try_Convert -> Try_Parse