Round up or down II

  • Comments posted to this topic are about the item Round up or down II


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Great question! It makes you think and it learns you something.

    The explanation was very well done.

    Good job!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • thanks 🙂

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

  • . makes a difference.

  • thanks for the question and the great explanation...

    Prashant Bhatt
    Sr Engineer - Application Programming

  • 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,

    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

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hi,

    Thank you very much for your explanation.

    Thanks a lot,

    a.shyam

  • Great question and even better explanation. Thank You.

  • Ah, the decimal point will get you every time. 😉

    I missed it in my first read.

  • Hugo, very good question.

    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 my opinion, the QOD should be able to serve multiple venues. Those of making one think, providing a learning ability as well as honing day to day skills. Unfortunately, the reading ability seems to be of the least importance, based upn the number of QOD's which are provided with typo's. Typo's have become the norm rather than a part of the test, and thus when the typo is intentional it becomes lost.

    Many times, as a DBA, one is tasked to review code from multiple developers who work at multiple levels. I am sure that it can be agreed to that if provided with the same task, 50 developers will submit at least 25 different ways to accomplish the goal based upon their skill set. Thus, one must be able to read at each of these levels in order to interpret as well as guide them in the appropriate direction, and thus the learning continues;-)

    Thanks for the question.

    Steve Jimmo
    Sr DBA
    “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan

  • Hugo, nice question and very nice job on explaining it!

    _______________________________________________________________________
    For better assistance in answering your questions, click here[/url]

Viewing 15 posts - 1 through 15 (of 26 total)

You must be logged in to reply to this topic. Login to reply