Convert VARCHAR to INT SQL Server

Posted on : 24-04-2012 | By : Devi Prasad | In : SQL Server Basics

Share:

0


Conversion from varchar to int (integer) can be done using CAST or CONVERT.

Below peice of code converts varchar to int using CAST.

DECLARE @intVariable INT
DECLARE @VarcharVariable VARCHAR(10)

SET @VarcharVariable = '99999'

--Converting VARCHAR TO INT Using CAST
SET @intVariable = CAST(@VarcharVariable as int)

SELECT @intVariable , @VarcharVariable

Below peice of code converts varchar to int using CONVERT.

DECLARE @intVariable INT
DECLARE @VarcharVariable VARCHAR(10)

SET @VarcharVariable = '999999'

--Converting VARCHAR TO INT Using Convert
SET @intVariable = CONVERT(int,@VarcharVariable)

SELECT @intVariable , @VarcharVariable

If you have non numeric characters use this link to convert string to number:
http://sqlserverlearner.com/2012/script-to-strip-non-numeric-characters-from-string

Applies to:
SQL Server 2012
SQL Server 2008
SQL Server 2008 r2
SQL Server 2005




References : Devi Prasad (sqlserverlearner.com)

Need Help On SQL Server?

Cannot Find Solution to your problem (or) If you are looking for some help on SQL Server. Dont worry Click Here to Post your question and solve your issue.


Do you like my blog?

If you liked reading this blog, please help spread the word by sharing this blog with your friends.




Write a comment



Tags: , , , , , , , , , , , ,