How to Do Type Casting in MS-SQL

Casting operations allow us to change the data type of a given value. Not all the casts are allowed, for example, you can’t cast ABC to integer, obviously.

In MS-SQL Server there you can use the cast(value as type) function to convert a data type:

-- Cast decimal to integer
select cast(2.0 AS integer)

-- Cast decimal to varchar
select cast(123456 AS varchar)

-- Cast a number to decimal with a specific number of decimal positions
select cast(55.95 as dec(3,0));

-- Cast a string representing a integer number to integer
select cast(‘1234’ as int)