﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Hugo Kornelis  / Round up or down III / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 18:34:32 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Nice question and resources on the rounding.  Thanks.</description><pubDate>Wed, 07 Mar 2012 14:04:13 GMT</pubDate><dc:creator>zymos</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>nice question but setting arithabort on gives completely diff solution</description><pubDate>Wed, 01 Jun 2011 04:31:05 GMT</pubDate><dc:creator>khullargirish02</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]terrykzncs (4/22/2011)[/b][hr]So... in the following code [code] declare @result decimal(12,8);set @result = round (5/3.0, 4,3);select @result;[/code]I get 1.66660000The operation (5/3.0) completes and the rounding is done according to the variable declared (decimal(12,8) - 1.66666667 -  and then truncated to 4 places - 1.6666?Sorry to be a pain, but how is it that I get 1.66660000 instead of 1.66670000? Which step decides if I get the 7 or 6 at the end?:unsure:[/quote]It is because of the extra ",3" in your ROUND call. [code="sql"]set @result = round (5/3.0, 4[b],3[/b]);[/code]The third parameter tells round to truncate instead of round if it is non-zero.Try this and I think you will get what you are looking for.:[code="sql"]declare @result decimal(12,8);set @result = round (5/3.0, 4);select @result;[/code]</description><pubDate>Mon, 25 Apr 2011 10:08:55 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>So... in the following code [code] declare @result decimal(12,8);set @result = round (5/3.0, 4,3);select @result;[/code]I get 1.66660000The operation (5/3.0) completes and the rounding is done according to the variable declared (decimal(12,8) - 1.66666667 -  and then truncated to 4 places - 1.6666?Sorry to be a pain, but how is it that I get 1.66660000 instead of 1.66670000? Which step decides if I get the 7 or 6 at the end?:unsure:</description><pubDate>Fri, 22 Apr 2011 12:33:12 GMT</pubDate><dc:creator>terrykzncs</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]Hugo Kornelis (9/12/2010)[/b][hr]On SQL Server 2005, the output of sp_help indicates that the column is typed as numeric(8,6), not numeric(38,6). I cannot check this on SQL Server 2008, as I don't have that version installed on this computer. Maybe you can execute this code on SQL 2008 and report back the results?[/quote]On SQL 2008 it is also numeric(8,6)</description><pubDate>Mon, 13 Sep 2010 02:20:17 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]Hugo Kornelis (9/12/2010)[/b][hr]However, I did not just post without checking. Here is how I checked my statement about the data type returned by the ROUND function:[code="sql"]SELECT ROUND(5/3.0, 1, 2) AS aINTO   b;goEXEC sp_help b;goDROP TABLE b;go[/code]On SQL Server 2005, the output of sp_help indicates that the column is typed as numeric(8,6), not numeric(38,6). I cannot check this on SQL Server 2008, as I don't have that version installed on this computer. Maybe you can execute this code on SQL 2008 and report back the results?)[/quote]that intrigued me, so I did some more documentation checking.It appears that there was a change between SQLS 2005 and SQL S2008; the 2005 version of BoL ([url]http://msdn.microsoft.com/en-us/library/ms175003(v=SQL.90).aspx[/url]) says that the result of ROUND has the same type as the expression to be rounded, as in your explanation.  The 2008 version ([url]http://msdn.microsoft.com/en-us/library/ms175003(v=SQL.100).aspx[/url]) and the current version ([url]http://msdn.microsoft.com/en-us/library/ms175003.aspx[/url]) both say that for neumerics and decimals the result type is decimal with precision 38.  So the explanation you gave was apparently correct for SQL 2005, and incorrect for SQL 2008.I haven't been able to check the 2008 behavious as I don't yet have SQL on the machine I have with me and am having difficulty getting hold of SQL server at a sensible price. My usual UK suppliers are quoting more for shipping a disc here than the total for licence, disc, and shipping if I were in the UK, so the total price ends up about 2.5 times what it would have been if I'd fixed it last week before I flew out; so I'm looking for a local supplier and in parallel trying to persuade of of the usual suppliers to ship be airmail instead of using UPS or any of the other overpriced and unreliable delivery companies that are so much in vogue in the computing industry; of course if I were in the US I could just buy online and download, but MS doesn't give me that option here.  The people I buy other stuff from [books, audio discs, dvd discs]  ship to here cheaply using the international mail system instead of fashionable private delivery companies, so I foolishly expected the same from software vendors.  I'll try to remember to run your test code when I get the license sorted and the software installed.</description><pubDate>Sun, 12 Sep 2010 13:16:20 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]Tom.Thomson (9/12/2010)[/b][hr]I think there's a small error in the explanation here. Round never returns a result of type decimal(8,6) because every decimal result that it returns has precision 38 - the datatype for decimal and numeric types is changed by round to decimal(38,s) where s is the scale of the original type. This is documented quite clearly in BoL ([url]http://msdn.microsoft.com/en-us/library/ms175003.aspx[/url]). So it will return a value of type decimal(38,6) which is then implicitly converted to decimal(5,2) for the assignment.[/quote]Hi Tom,Thanks for catching that. I can not believe I actually overlooked that (though the hyperlink is damaged, I did include a link to that article in the explanation).However, I did not just post without checking. Here is how I checked my statement about the data type returned by the ROUND function:[code="sql"]SELECT ROUND(5/3.0, 1, 2) AS aINTO   b;goEXEC sp_help b;goDROP TABLE b;go[/code]On SQL Server 2005, the output of sp_help indicates that the column is typed as numeric(8,6), not numeric(38,6). I cannot check this on SQL Server 2008, as I don't have that version installed on this computer. Maybe you can execute this code on SQL 2008 and report back the results?(Of course, for this particular question it does not really matter; there is no overflow so the numeric(38,6) would just add 30 extra leading zeroes and not affect the result - but now that you found this, I do want to go to the bottom of it;-))</description><pubDate>Sun, 12 Sep 2010 06:42:12 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>I think there's a small error in the explanation here. Round never returns a result of type decimal(8,6) because every decimal result that it returns has precision 38 - the datatype for decimal and numeric types is changed by round to decimal(38,s) where s is the scale of the original type. This is documented quite clearly in BoL ([url]http://msdn.microsoft.com/en-us/library/ms175003.aspx[/url]). So it will return a value of type decimal(38,6) which is then implicitly converted to decimal(5,2) for the assignment.</description><pubDate>Sun, 12 Sep 2010 06:22:55 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Agreed - Hugo, great question.  I'm fascinated with the way the round works in SQL.</description><pubDate>Sun, 29 Aug 2010 07:14:14 GMT</pubDate><dc:creator>Jamie Longstreet-481950</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Coool question, Hugo.. Thanks :-)</description><pubDate>Wed, 25 Aug 2010 08:19:34 GMT</pubDate><dc:creator>ColdCoffee</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]jts_2003 (8/17/2010)[/b][hr]So would it be fair to say that the third argument of ROUND, function, actually ensures you round down instead of up, if you set it to a non zero value?[/quote]No. The third argument is to truncate instead of rounding. Rounding can be both up and down (when rounding to the integer, 1.7 will be rounded up but 1.3 will be rounded down). But truncation can also have the effect of rounding either up or down - truncating 1.7 will result in 1 (down), but truncating -1.7 will result in -1 (up).To round up or down, use CEILING() and FLOOR(). To round according to normal rounding rules, use ROUND() with the default function. And to truncate, use ROUND() with the non-default function.[quote][b]john.moreno (8/17/2010)[/b][hr]Nope.  It truncates and then does the rounding.  ROUND(5/3.0, 3,2) would have produced a result of 1.67 (up from the nominal value of 1.666666).[/quote]The ROUND() function with the non-zero function actually only truncates. The result of ROUND(5/3.0, 3, 2) is 1.666000 - truncated to the third decimal. This can be verified by running SELECT ROUND(5/3.0, 3, 2);When you assign that result to a variable that is declared to have two decimal places (as in the code used in the QotD), the assignment forces an implicit conversion. Normal rounding rules apply, so the result will then be 1.67. But that is not related to the ROUND() function itself.</description><pubDate>Tue, 17 Aug 2010 14:45:22 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]jts_2003 (8/17/2010)[/b][hr]So would it be fair to say that the third argument of ROUND, function, actually ensures you round down instead of up, if you set it to a non zero value?[/quote]Nope.  It truncates and then does the rounding.  ROUND(5/3.0, 3,2) would have produced a result of 1.67 (up from the nominal value of 1.666666).</description><pubDate>Tue, 17 Aug 2010 11:50:58 GMT</pubDate><dc:creator>john.moreno</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>So would it be fair to say that the third argument of ROUND, function, actually ensures you round down instead of up, if you set it to a non zero value?</description><pubDate>Tue, 17 Aug 2010 10:25:24 GMT</pubDate><dc:creator>jts_2003</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Thanks for the question Hugo.  Nice continuation in the series.</description><pubDate>Tue, 17 Aug 2010 08:53:28 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>I went 0 for 3 on the rounding questions. Scary. Well, what was really scary was that I didn't read through the ROUND documentation after the first question, which no doubt would have helped for the next 2 questions I encountered. I will make sure to read through the references more carefully from now on.They were all great questions, though, and progressed in a way where a new twist was introduced for each one. Nice work.Thanks,webrunner</description><pubDate>Tue, 17 Aug 2010 08:49:42 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]Toreador (8/17/2010)[/b][hr]So why not just make it a Bit datatype and call it 'Truncate' instead of 'Function'? Did it original behave differently?[/quote]Good question. And I have no idea.I did check in the SQL Server 2000 version of Books Online (the oldest version I was able to find online), and it was the same then.Maybe it's for ANSI compliance? (the SQL Server BIT datatype is not ANSI-standard)</description><pubDate>Tue, 17 Aug 2010 08:21:18 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Hugo Kornelis Copied and tested your code and got your answers. Unfortunately I did not save my test code and now can not duplicate my results, and I tested that not less than 5 times ......... darn if I know the difference ..My apologies to all ...... will do some further investigation to see if I can discover what [b] I did incorrectly.[/b]Again sorry - - I did edit my post so as not to pass on what is now not correct information.</description><pubDate>Tue, 17 Aug 2010 08:18:57 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Interesting question and good explanation.For a normal person 3 and 3.0 is same.  programming can be manipulative.</description><pubDate>Tue, 17 Aug 2010 08:16:05 GMT</pubDate><dc:creator>SanjayAttray</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Interesting.Can anyone explain exactly what the 3rd argument of Round() is for?The BOL says"function Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated."which in practice seems to mean that 0 means round, any other value means truncate. SoSET @Result = ROUND(5/3.0, 1, 2);SET @Result = ROUND(5/3.0, 1, 1);SET @Result = ROUND(5/3.0, 1, 999999999);SET @Result = ROUND(5/3.0, 1, -12345.6789);all do the same thing.So why not just make it a Bit datatype and call it 'Truncate' instead of 'Function'? Did it original behave differently?</description><pubDate>Tue, 17 Aug 2010 08:08:40 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>[quote][b]bitbucket-25253 (8/17/2010)[/b][hr]For a completly different answer - execute the code with the setting forARITHABORT ON and note the difference.  An important fact to be aware of.[/quote]Huh? I executed this code:[code="sql"]SET ARITHABORT ON;DECLARE @Result decimal(5,2);SET @Result = ROUND(5/3.0, 1, 2);PRINT @Result;goSET ARITHABORT OFF;DECLARE @Result decimal(5,2);SET @Result = ROUND(5/3.0, 1, 2);PRINT @Result;[/code]And I got 1.60 as both results. What did you get?</description><pubDate>Tue, 17 Aug 2010 07:14:19 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Great question, thanks.  If I keep seeing order of precedence questions on QoTD, I will eventually have it memorized, and won't need to look it up any more.</description><pubDate>Tue, 17 Aug 2010 06:42:59 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>For a completly different answer - execute the code with the setting forARITHABORT ON and note the difference.  An important fact to be aware of.Added after reading Hugo Kornelis Posted Today @ 9:14 AM postingCopied and tested his code and got his answers.  Unfortunately I did not save my test code and now can not duplicate my results, and I tested not less than 5 times ......... darn if I know the difference ..[B]SO ALL PLEASE DISREGARD MY COMMENTS ABOUT GETTING A DIFFERENT ANSWER WITH ARITHABORT SETTING![/B]</description><pubDate>Tue, 17 Aug 2010 06:39:07 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Good one. Made me look it up.Thanks.</description><pubDate>Tue, 17 Aug 2010 06:30:10 GMT</pubDate><dc:creator>Tom Garth</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Good question Hugo, learned something new.</description><pubDate>Tue, 17 Aug 2010 06:27:56 GMT</pubDate><dc:creator>jcrawf02</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>interesting...</description><pubDate>Tue, 17 Aug 2010 02:36:21 GMT</pubDate><dc:creator>Yuvaraj S-467280</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Very good as usual:)</description><pubDate>Tue, 17 Aug 2010 00:26:59 GMT</pubDate><dc:creator>Christian Buettner-167247</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Nice question, thanks. A good follow up of the previous rounding questions.And a great explanation.</description><pubDate>Mon, 16 Aug 2010 23:52:54 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Nice question, thanks! I do find it interesting that they combined rounding and truncating in one function...</description><pubDate>Mon, 16 Aug 2010 22:32:41 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>Round up or down III</title><link>http://www.sqlservercentral.com/Forums/Topic970125-1328-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/70325/"&gt;Round up or down III&lt;/A&gt;[/B]</description><pubDate>Mon, 16 Aug 2010 21:48:11 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item></channel></rss>