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.

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

SELECT CAST('123' as INT)

Watch a course Learn object oriented PHP

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 return an error. You can use ISNUMERIC() function to check whether it's numeric value or not before casting.

DECLARE @varcharValue VARCHAR(100) = '123.45'
IF (ISNUMERIC(@varcharValue) = 1)
BEGIN
    SELECT CAST(@varcharValue as DECIMAL(10, 2))
END
ELSE
BEGIN
    SELECT 'Invalid input'
END