dbTalk Databases Forums  

Converting Varchar to Numeric/Decimal/Float

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Converting Varchar to Numeric/Decimal/Float in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
danmeka@gmail.com
 
Posts: n/a

Default Converting Varchar to Numeric/Decimal/Float - 02-21-2006 , 11:13 AM






I'm trying to take a varchar field and convert it to
numeric/decimal/float (not sure which is the best data type). I keep
receiving an error on the conversion:

Server: Msg 8114, Level 16, State 5, Line 4
Error converting data type varchar to numeric.

This is the code I tried to use but seems to be problematic:

case when ltrim(rtrim(ltv)) <> ''
then cast(ltv as numeric(21,20))
else NULL end as ltv,

The data in the field I believe to be the issue looks like this:

5.9499999999999997E-2

It seems to be a problem with converting the scientific notation, but I
don't know how to handle that. Any ideas?


Reply With Quote
  #2  
Old   
SQL
 
Posts: n/a

Default Re: Converting Varchar to Numeric/Decimal/Float - 02-21-2006 , 11:44 AM






convert to float first

doesn't work
select convert(numeric(21,20),'5.9499999999999997E-2')

works
select convert(numeric(21,20),convert(float,'5.9499999999 999997E-2'))

http://sqlservercode.blogspot.com/


Reply With Quote
  #3  
Old   
SQL
 
Posts: n/a

Default Re: Converting Varchar to Numeric/Decimal/Float - 02-21-2006 , 01:03 PM



convert to float first

doesn't work
select convert(numeric(21,20),'5.9499999999999997E-2')

works
select convert(numeric(21,20),convert(float,'5.9499999999 999997E-2'))

http://sqlservercode.blogspot.com/


Reply With Quote
  #4  
Old   
danmeka@gmail.com
 
Posts: n/a

Default Re: Converting Varchar to Numeric/Decimal/Float - 02-21-2006 , 02:50 PM



Great, thanks! That definitely has solved my problem. I appreciate
the help.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.