dbTalk Databases Forums  

Convert character data with "-" negative sign to numeric

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


Discuss Convert character data with "-" negative sign to numeric in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gary
 
Posts: n/a

Default Convert character data with "-" negative sign to numeric - 02-16-2005 , 12:31 PM






I have character data (money) that I need to convert to numeric data type.

When I use the convert or cast functions I receive, " Error converting data
type varchar to numeric." error.

I found that the character data contains credits with a negative sign. Does
anyone have a good idea how to convert this to numeric or decimal format?

Reply With Quote
  #2  
Old   
Jim Young
 
Posts: n/a

Default Re: Convert character data with "-" negative sign to numeric - 02-16-2005 , 01:46 PM






Is it possible that there are some other characters causing the conversion
error?

A test like SELECT CAST('-34.00' as MONEY) works just fine and returns

-34.0000

Could you post a sample of the data?

Jim

"Gary" <Gary (AT) discussions (DOT) microsoft.com> wrote

Quote:
I have character data (money) that I need to convert to numeric data type.

When I use the convert or cast functions I receive, " Error converting
data
type varchar to numeric." error.

I found that the character data contains credits with a negative sign.
Does
anyone have a good idea how to convert this to numeric or decimal format?



Reply With Quote
  #3  
Old   
Sue Hoegemeier
 
Posts: n/a

Default Re: Convert character data with "-" negative sign to numeric - 02-16-2005 , 02:01 PM



If you just need to strip out the negative sign, try
something like:
cast(replace(YourTextColumn, '-', '') as decimal(10, 2))

-Sue

On Wed, 16 Feb 2005 10:31:02 -0800, "Gary"
<Gary (AT) discussions (DOT) microsoft.com> wrote:

Quote:
I have character data (money) that I need to convert to numeric data type.

When I use the convert or cast functions I receive, " Error converting data
type varchar to numeric." error.

I found that the character data contains credits with a negative sign. Does
anyone have a good idea how to convert this to numeric or decimal format?


Reply With Quote
  #4  
Old   
Gary
 
Posts: n/a

Default Re: Convert character data with "-" negative sign to numeric - 02-16-2005 , 02:07 PM



Thank for your reply Jim.

The negative sign was on the right. Convert and cast only work if the "-"
is on the left.

I did resolve it though. I just moved the "-" from the right to the left
with this:

RIGHT(RTRIM(freight),1) + SUBSTRING(freight,1,len(freight)-1)

Thanks again.


"Jim Young" wrote:

Quote:
Is it possible that there are some other characters causing the conversion
error?

A test like SELECT CAST('-34.00' as MONEY) works just fine and returns

-34.0000

Could you post a sample of the data?

Jim

"Gary" <Gary (AT) discussions (DOT) microsoft.com> wrote in message
news:9809CB74-4934-4107-AD27-3E9B360C9CDC (AT) microsoft (DOT) com...
I have character data (money) that I need to convert to numeric data type.

When I use the convert or cast functions I receive, " Error converting
data
type varchar to numeric." error.

I found that the character data contains credits with a negative sign.
Does
anyone have a good idea how to convert this to numeric or decimal format?




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.