June 28, 2010 at 10:01 am
We have a nvarchar field that has negative numbers in it (-10,299.00) which is correct.
But we need to convert this to a decimal to be able to add/subtract with another decimal field.
We can't seem to make this work. Is there something special because it has a negative value?
June 28, 2010 at 10:04 am
Yes drop the comma. Make sure to always use the period for decimal separator. Then the convert should take.
June 28, 2010 at 10:06 am
Sorry it doesn't have commas (my mistake)
so how do we handle the period?
June 28, 2010 at 10:07 am
Try this.
declare @amt nvarchar(100)
set @amt = '-10299.00'
select convert(decimal,REPLACE(@amt,',','')) as Result
--Result -10299
June 28, 2010 at 10:11 am
Then please post sample data that is failing so that we can see the problem.
June 28, 2010 at 10:14 am
select cast(replace(replace(replace('(-10,299.00)', '(',''),')',''), ',','') as decimal(18,2))
Do you have brackets? Can you show exact value you are trying to convert?
June 28, 2010 at 10:19 am
thanks for answering everyone...
here's some sample data -
-95469.49
-92565.24
-90384.38
-89245.69
-88976.89
-88976.89
-82735.84
-82735.84
-79358.26
-78217.29
June 28, 2010 at 10:33 am
I can't see any problem there:
declare @t table (val nvarchar(50))
insert into @t select '-95469.49'
union select '-92565.24'
union select '-90384.38'
union select '-89245.69'
union select '-88976.89'
union select '-88976.89'
union select '-82735.84'
union select '-82735.84'
union select '-79358.26'
union select '-78217.29'
select CAST(val as decimal(18,2)) from @t
The above works just fine!
What code you have? What error you get?
June 28, 2010 at 10:38 am
can you post the result of this query,
select convert(decimal,'-95469.49') as Result
June 28, 2010 at 10:40 am
declare @amt nvarchar(60)
set @amt = '-10299.50'
select convert(decimal(9,2), @amt ) as Result
Thanks [/font]
June 28, 2010 at 12:16 pm
We figured it out
there was a few empty values in that field, if we update to zero it worked fine.
thanks everyone!
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply