How to convert Varchar to Int in sql server 2008.
i have following code when i tried to run it wont allowed me to convert Varchar to Int.
Select Cast([Column1] as INT)
Column1 is of Varchar(21) NOT NULL type and i wanted to convert it into Int.
actually i am trying to insert Column1 into another table having Field as INT.
can someone please help me to convert this ?
Spaces will not be a problem for
cast, however characters likeTAB,CRorLFwill appear as spaces, will not be trimmed byLTRIMorRTRIM, and will be a problem.For example try the following:
Check your input for these characters and if you find them, use
REPLACEto clean up your data.Per your comment, you can use
REPLACEas part of yourcast:If this is something that will be happening often, it would be better to clean up the data and modify the way the table is populated to remove the
CRandLFbefore it is entered.