Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Round up or down II Expand / Collapse
Author
Message
Posted Monday, August 9, 2010 9:09 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
Comments posted to this topic are about the item Round up or down II


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #966339
Posted Tuesday, August 10, 2010 12:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:44 AM
Points: 13,638, Visits: 10,528
Great question! It makes you think and it learns you something.
The explanation was very well done.

Good job!




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #966378
Posted Tuesday, August 10, 2010 12:25 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, June 30, 2014 2:51 AM
Points: 2,017, Visits: 369
thanks

was always worried about such integer conversions... will now just add a dot to ensure that the conversion is correct...
Post #966379
Posted Tuesday, August 10, 2010 1:11 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 4:17 AM
Points: 317, Visits: 95
. makes a difference.

Post #966392
Posted Tuesday, August 10, 2010 1:26 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 7:16 AM
Points: 118, Visits: 174
thanks for the question and the great explanation...

Prashant Bhatt
Sr Engineer - Application Programming
Post #966397
Posted Tuesday, August 10, 2010 2:44 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 10, 2014 4:46 AM
Points: 39, Visits: 43
H(o)i Hugo,

Oeps, weer wat geleerd.

The precedence of decimal is higher than that of integer, so the result will be truncated to a decimal(7,6) value of 1.666666.

This is probably a very newbie question, but I am never afraid to ask newbie questions--so here goes: is decimal(7,6) some sort of default?

Best regards,

Ronald
Post #966447
Posted Tuesday, August 10, 2010 2:46 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, August 24, 2014 8:26 PM
Points: 435, Visits: 89
Hi,
Very interesting question.
But I cant understand the usage of the 'dot' in the line
Set @result = Round(5/3.,1)
Can you please explain.

Regards,
a.shyam
Post #966448
Posted Tuesday, August 10, 2010 3:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
ziangij (8/10/2010)
thanks

was always worried about such integer conversions... will now just add a dot to ensure that the conversion is correct...

You're welcome.
But please do realise that the code in this code is deliberately obscure. (Reading my question now, many weeks after submitting it, I am wondering if I even should have written it like this - QotD should test SQL understanding, not reading ability).
In production code, I recommend against using only the dot, as it's easily overlooked. Please use at least ".0". Or use an explicit CAST.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #966471
Posted Tuesday, August 10, 2010 3:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
Ronald Beuker (8/10/2010)
H(o)i Hugo,

Oeps, weer wat geleerd.

The precedence of decimal is higher than that of integer, so the result will be truncated to a decimal(7,6) value of 1.666666.

This is probably a very newbie question, but I am never afraid to ask newbie questions--so here goes: is decimal(7,6) some sort of default?

Best regards,

Ronald

Hi Ronald,
Long time no see!

Your question is not a newbie question at all. In fact, this touches on a subject that often leaves seasoned experts looking bewildered at their query results.

What happens is that, as stated in the explanation, data type precedence determines that decimal has a higher precendence then int, so the int argument is converted to decimal. In this case, probably decimal(1,0) but I'm not 100% sure and I can't check. Now both arguments are of the decimal type.

The official explanation of the next step is here: http://msdn.microsoft.com/en-us/library/ms190476.aspx. The operation used here is division, so the precision (p) of the result is determined as p1 - s1 + s2 + max(6, s1 + p2 + 1), and the scale (s) as max(6, s1 + p2 + 1). Since both operands are decimal (1,0), p1 and p2 are both 1; and s1 and s2 are both 0. If you substitute those numbers in the formulas, you'll see that the results are 7 for precision and 6 for scale.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #966476
Posted Tuesday, August 10, 2010 3:33 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
shyamsri83 (8/10/2010)
Hi,
Very interesting question.
But I cant understand the usage of the 'dot' in the line
Set @result = Round(5/3.,1)
Can you please explain.

Regards,
a.shyam

Adding the dot is the easiest (but also the most obscure) way to ensure that SQL Server will consider a numeric constant to be of data type decimal rather than int. But as already mentioned above, in real code I'd recommend one of these more legible alternatives:
ROUND (5 / 3.0)
ROUND (5 / CAST(3 AS numeric(1,0))



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #966478
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse