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
 General SQL Server Forums
 New to SQL Server Programming
 converting a varchar to a numeric data type

Author  Topic 

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-05-25 : 16:45:49
what is the syntax to convert a varchar datatype to numeric in a join statement?

Roger DeFour

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-25 : 17:06:07
convert(<numeric data type>,varchar_field)
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-05-26 : 03:53:43
quote:
Originally posted by vijayisonly

convert(<numeric data type>,varchar_field)



SELECT CONVERT(INT, CASE WHEN ISNUMERIC(<varchar column>) = 1 THEN <varchar column> ELSE NULL ) FROM YourTable

you can use IsNumeric to avoid error while conversion for unexpected wrong data.

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-26 : 04:03:19
quote:
Originally posted by vaibhavktiwari83

quote:
Originally posted by vijayisonly

convert(<numeric data type>,varchar_field)



SELECT CONVERT(INT, CASE WHEN ISNUMERIC(<varchar column>) = 1 THEN <varchar column> ELSE NULL ) FROM YourTable

you can use IsNumeric to avoid error while conversion for unexpected wrong data.

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER


Note that isnumeric is not reliable
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/enhanced-isnumeric-function.aspx

Madhivanan

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

- Advertisement -