|
|
Question : Casting Char to Decimal
|
|
I'm trying to pull shipping costs to sum out of our database and for some genius reason the database developers made the cost field a char field so i can not sum them. Currently i use code to cast it to decimal type and i can sum but i noticed it is rounding the cost field. For instance the first record should add 4.93 and 7.05 for 11.98 total, but instead it's adding 5 and 7 and i'm getting 12. Is ther anyway i can pull the data out and sum it without using rounded numbers?
Here is my code:
select upstable.shipnumber,sum(cast(upstable.cost as decimal)) from sotable,shtable,upstable where left(pono,2) = 'ww' and shipdate between ('4/1/08') and ('4/30/08') and void <> 'y' and shtable.shipnumber = upstable.shipnumber and sotable.sonumber= shiptable.shipnumber group by upstable.shipnumber order by upstable.shipnumber
|
Answer : Casting Char to Decimal
|
|
Yes, need to give it precision. and put in extra checks - just incase...
try sum(convert(decimal(18,5),case when isnumeric(upstable.cost) > 0 then upstable.cost else 0 end))
|
|
|
|
|