Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 cast string to int

Author  Topic 

linda9898
Starting Member

28 Posts

Posted - 2008-04-27 : 03:19:24
Hi ,

I have a problem with casting a string number to int.
for exmaple i have the number '0110' (string) and i would like it to be 110. or '0001' to be 1.
i tried to do:
cast(MyNUM as int) .
but i get "Syntax error converting the nvarchar value 'O322' to a column of data type int."

is anyone have any idea how to solve it ?

thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-27 : 04:04:09
I dont think its a 0 in begining i think its english alphabet O as per error info.

if you need to get only the number from it. use:-

SELECT CAST(REPLACE(MyNUM,'O','') AS int)
Go to Top of Page

linda9898
Starting Member

28 Posts

Posted - 2008-04-27 : 04:07:29
it works now , thanks alottttttttttt
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-28 : 03:44:07
In case if you have any other alphabets, use
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/18/extract-only-numbers-from-a-string.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -