Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Fun with Scale and Precision Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, December 3, 2009 10:38 PM
 SSChampion Group: General Forum Members Last Login: Friday, May 18, 2007 3:36 PM Points: 10,041, Visits: 1
 Comments posted to this topic are about the item Fun with Scale and Precision
Post #828643
 Posted Thursday, December 3, 2009 11:55 PM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 12:52 AM Points: 5,364, Visits: 7,073
 All good and well, however, the explanation re why the rounding off takes place is.....? ____________________________________________Space, the final frontier? not any more...All limits henceforth are self-imposed.“libera tute vulgaris ex”
Post #828655
 Posted Friday, December 4, 2009 12:38 AM
 Forum Newbie Group: General Forum Members Last Login: Friday, December 4, 2009 12:37 AM Points: 1, Visits: 0
 0.000167
Post #828667
 Posted Friday, December 4, 2009 1:28 AM
 Hall of Fame Group: General Forum Members Last Login: Saturday, June 6, 2015 9:44 PM Points: 3,450, Visits: 4,408
 stewartc-708166 (12/3/2009)the explanation re why the rounding off takes place is.....?Something about this can be found in BOL, topic "Precision, Scale, and Length (Transact-SQL)": http://technet.microsoft.com/en-us/library/ms190476.aspxWe have two numbers of type NUMERIC(38,10), so their precision = 38 and scale = 10.According to the table from the above link, the result precision is: p1 - s1 + s2 + max(6, s1 + p2 + 1) = 38 - 10 + 10 + max(6, 10 + 38 + 1) = 38 + 49 = 87.The result scale is: max(6, s1 + p2 + 1) = max(6, 10 + 38 + 1) = 49.But there is also a note:* The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.Ok, the result precision (87) is definitely greater than 38, so it was reduced to 38. But why the scale was reduced to 6 – I can't find any explanation
Post #828680
 Posted Friday, December 4, 2009 1:53 AM
 Ten Centuries Group: General Forum Members Last Login: Thursday, October 27, 2016 3:28 AM Points: 1,249, Visits: 1,150
 INterestingly, and counter-intuitively, at least for me ...SELECT cast(1.67574 as decimal(38,10)) /cast(10000 as decimal(38,10))gives this result: .000167While SELECT cast(1.67574 as decimal(38,10)) /cast(10000 as decimal(38,1))gives this more accurate result.000167574
Post #828686
 Posted Friday, December 4, 2009 2:48 AM
 SSC Veteran Group: General Forum Members Last Login: Yesterday @ 8:01 AM Points: 234, Visits: 313
 The missing digits is due to the initial casts - with decimal, they are automatically converted to the minimum numeric precision and scale required before the calculation is done, so 1.67574 ends up with a scale of 5 and 10000 with a scale of 0. I would have thought this would lead to an initial result with scale of 5 i.e. 0.00016 and a final result of 0.0001600000 so I'm wondering how the 6th digit gets retained. --Colin Frame
Post #828708
 Posted Friday, December 4, 2009 2:58 AM
 SSC Veteran Group: General Forum Members Last Login: Yesterday @ 8:01 AM Points: 234, Visits: 313
 This explains it:http://msdn.microsoft.com/en-us/library/aa258274(SQL.80).aspxFor division:[EDITED - was incorrect, apologies CFF] scale of the result = max(6, s1 + p2 + 1)where s1 is the scale of the numerator and p2 is the precision of the denominator. --Colin Frame
Post #828713
 Posted Friday, December 4, 2009 5:05 AM
 SSC Veteran Group: General Forum Members Last Login: Thursday, July 9, 2015 9:04 AM Points: 280, Visits: 94
 `declare @d1 decimal(38,10), @d2 decimal(38,10)SELECT @d1 = 1.67574, @d2 = 10000SELECT d1 = @d1, d2 = @d2, d1_div_d2 = @d1 / @d2, cast_div = CAST(@d1 / @d2 AS DECIMAL(38,10)), num_div = 1.67574 / 10000.0, cast_num_div = CAST(1.67574 / 10000.0 AS DECIMAL(38,10))`done (i trimed strings to results)`d1 d2 d1_div_d2 cast_div num_div cast_num_div------------ ---------------- --------- ------------ -------------- ------------1.6757400000 10000.0000000000 0.000167 0.0001670000 0.000167574000 0.0001675740` My MCP Transcript (ID : 692471 Access : 109741229)
Post #828761
 Posted Friday, December 4, 2009 7:10 AM
 Old Hand Group: General Forum Members Last Login: Tuesday, January 1, 2013 3:03 PM Points: 317, Visits: 1,018
 colin.frame (12/4/2009)This explains it:http://msdn.microsoft.com/en-us/library/aa258274(SQL.80).aspxFor division: scale of the result = s1 + s2 + 1where s1 and s2 are the scales of the initial numbers.Nowhere is "s1+s2+1" mentioned in the link provided.
Post #828880
 Posted Friday, December 4, 2009 7:26 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Friday, August 26, 2016 9:17 AM Points: 116, Visits: 58
 Odd that replacing the CAST(10000,NUMERIC(38,10)) with 10000 gives the more precise result 0.0001675740.Also odd that Sybase 15.0.2 gives the result 0.0001675740 with the original query. You'd think running the SAME query on two ANSI-standard DBMSs would give the SAME result.
Post #828900

 Permissions