W3docs

Converting Varchar Value to Integer/Decimal Value in SQL Server

In SQL Server, you can convert a varchar value to an integer or decimal value using the CAST or CONVERT function.

In SQL Server, you can convert a varchar value to an integer or decimal value using the CAST or CONVERT function.

To convert a varchar value to an integer, you can use the CAST function like this:

Example of CAST function in SQL

SELECT CAST('123' AS INT)

To convert a varchar value to a decimal, you can use the CAST or CONVERT function like this:


SELECT CAST('123.45' AS DECIMAL(10, 2))

or

SELECT CONVERT(DECIMAL(10, 2), '123.45')

Please note that if the varchar value cannot be converted to the specified data type, the CAST or CONVERT function will throw an error. Starting with SQL Server 2012, it is recommended to use TRY_CAST or TRY_CONVERT instead of ISNUMERIC(). These functions return NULL on failure rather than raising an error, and they correctly identify numeric values without the false positives of ISNUMERIC().


DECLARE @varcharValue VARCHAR(100) = '123.45';
SELECT TRY_CAST(@varcharValue AS DECIMAL(10, 2)) AS ConvertedValue;