{"id":173,"date":"2012-04-24T09:17:52","date_gmt":"2012-04-24T09:17:52","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/04\/24\/convert-varchar-to-int-sql-server\/"},"modified":"2012-04-24T09:17:52","modified_gmt":"2012-04-24T09:17:52","slug":"convert-varchar-to-int-sql-server","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/04\/24\/convert-varchar-to-int-sql-server\/","title":{"rendered":"Convert VARCHAR to INT SQL Server"},"content":{"rendered":"

Conversion from varchar to int (integer) can be done using CAST or CONVERT.<\/p>\n

Below peice of code converts varchar to int using CAST.<\/p>\n

[sql]
\nDECLARE @intVariable INT
\nDECLARE @VarcharVariable VARCHAR(10)<\/p>\n

SET @VarcharVariable = ‘99999’<\/p>\n

–Converting VARCHAR TO INT Using CAST
\nSET @intVariable = CAST(@VarcharVariable as int)<\/p>\n

SELECT @intVariable , @VarcharVariable
\n[\/sql]<\/p>\n

Below peice of code converts varchar to int using CONVERT.
\n[sql]
\nDECLARE @intVariable INT
\nDECLARE @VarcharVariable VARCHAR(10)<\/p>\n

SET @VarcharVariable = ‘999999’<\/p>\n

–Converting VARCHAR TO INT Using Convert
\nSET @intVariable = CONVERT(int,@VarcharVariable)<\/p>\n

SELECT @intVariable , @VarcharVariable
\n[\/sql]<\/p>\n

If you have non numeric characters use this link to convert string to number:
\nhttp:\/\/sqlserverlearner.com\/2012\/script-to-strip-non-numeric-characters-from-string<\/a><\/p>\n

Applies to:
\nSQL Server 2012
\nSQL Server 2008
\nSQL Server 2008 r2
\nSQL Server 2005<\/p>\n","protected":false},"excerpt":{"rendered":"

Conversion from varchar to int (integer) can be done using CAST or CONVERT. Below peice of code converts varchar to int using CAST. [sql] 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 [\/sql] Below peice of code…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[1776,1777,1778,1779,1780,1781,1782,1783,1784,1785,1786,1787,1788],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/173"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}