Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Round up or down II


Round up or down II

Author
Message
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8738 Visits: 11716
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
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18483 Visits: 13247
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?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
ziangij
ziangij
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2977 Visits: 376
thanks :-)

was always worried about such integer conversions... will now just add a dot to ensure that the conversion is correct...
yogender
yogender
Old Hand
Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)

Group: General Forum Members
Points: 393 Visits: 131
. makes a difference.
prashant.bhatt
prashant.bhatt
SSC-Enthusiastic
SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)

Group: General Forum Members
Points: 122 Visits: 177
thanks for the question and the great explanation...

Prashant Bhatt
Sr Engineer - Application Programming
Ronald Beuker
Ronald Beuker
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
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
shyamsri83
shyamsri83
SSC-Addicted
SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)SSC-Addicted (438 reputation)

Group: General Forum Members
Points: 438 Visits: 149
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
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8738 Visits: 11716
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
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8738 Visits: 11716
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
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8738 Visits: 11716
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search