﻿<?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 Julie Zeien  / Fun with Decimals / 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>Sat, 25 May 2013 22:06:10 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>Great question Julie, even if the explanation was not exactly right. I was unaware of this behaviour, and THAT is the point of asking and discussing it :)</description><pubDate>Sat, 20 Mar 2010 10:01:02 GMT</pubDate><dc:creator>Peter Trast</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>[quote][b]Julie Zeien (12/16/2009)[/b][hr]I apologize for the incorrect explanation.  I ran into a problem with this sort of thing about 3 months ago, and I looked all over BOL EXCEPT for the decimal data type page.  Oops.  I thought this was an undocumented problem, so I played with it for a couple hours and came to an incorrect conclusion.  Hopefully Steve can fix it.  Thank you to those who knew the correct reason behind this.[/quote]Unfortunately it's very easy play with something for a while, form a mental model of what's going on, then try a few tests to check the model and when they work believe it is true.  So don't worry to much at having done it - we all do it sometimes.  The great trick is to find some tests that will check all the edge conditions implied by the model, which is sometimes very hard to do.</description><pubDate>Sat, 06 Feb 2010 18:43:55 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>A better question would have been, what is the result of:SELECT CONVERT(DECIMAL, 1.2)</description><pubDate>Fri, 08 Jan 2010 11:50:53 GMT</pubDate><dc:creator>bvbellomo</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>The important point with this question is that you should never take anything for granted and always specify "optional" paramters/statements</description><pubDate>Fri, 08 Jan 2010 07:34:23 GMT</pubDate><dc:creator>hakan.winther</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>Julie, I've been through a similar experience posting a QOD and then finding out that I'd missed a point.  Don't fret over that.But, more important than a statement of the correct explanation for your results, folks should realize that NEITHER example is really a correct construction.  You said it yourself in the explanation, so let's see the real effect of not specifying precision.  This query:[code="sql"]DECLARE @TestDecimal DECIMAL(8, 2)SET @TestDecimal = 275953.73SELECT CONVERT( DECIMAL, @TestDecimal * 0.40 ) , CONVERT( DECIMAL, @TestDecimal ) * 0.40 , CONVERT( DECIMAL(8,2), @TestDecimal * 0.40 )[/code]returns [code="plain"]110381	110381.60	110381.49[/code]Only the last column, the one produced with an explicit precision, is correct. The CONVERT in the second column rounds the local variable 275953.73 to 275954 before doing the multiplication by 0.40.</description><pubDate>Thu, 17 Dec 2009 11:28:00 GMT</pubDate><dc:creator>john.arnott</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>I apologize for the incorrect explanation.  I ran into a problem with this sort of thing about 3 months ago, and I looked all over BOL EXCEPT for the decimal data type page.  Oops.  I thought this was an undocumented problem, so I played with it for a couple hours and came to an incorrect conclusion.  Hopefully Steve can fix it.  Thank you to those who knew the correct reason behind this.</description><pubDate>Wed, 16 Dec 2009 09:20:16 GMT</pubDate><dc:creator>Julie Hargraves</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>Correct answer, but the explanation is completely wrong!</description><pubDate>Wed, 16 Dec 2009 09:00:15 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>[quote][b]Bhavesh_Patel (12/15/2009)[/b][hr]Why answer differs when we are using the same function on same variable?[/quote]Because we're not actually running the function on the same variable. In the first column, we're doing the following:CONVERT(DECIMAL, @TestDecimal * 0.40)Procedurally, this is1. Multiply @TestDecimal by 0.402. Convert the result to DECIMAL, using the defaults (precision 18, scale 0). This is where the rounding occurs.In column 2, we're doingCONVERT(DECIMAL, @TestDecimal) * 0.40Procedurally, this is1. Convert the value in @TestDecimal to DECIMAL, using the defaults (precision 18, scale 0). Since there's nothing to the right of the decimal, the value is not changed (although the data type is.)2. Multiply this by 0.40. This creates a resulting value with digits to the right of the decimal point. See [url]http://technet.microsoft.com/en-us/library/ms190476.aspx[/url] for details on how SQL Server determines the precision and scale of a result of a mathematical operation on DECIMAL values.</description><pubDate>Wed, 16 Dec 2009 07:27:07 GMT</pubDate><dc:creator>sknox</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>[quote][b]Explanation[/b][hr]When precision is not specified with the decimal type, TSQL will use whatever precision would use the minimum space to store with the number it is converting to.[/quote]This is not correct. SQL Server does not use "whatever precision" in this case. Here is the quote from BOL about precision ([url]http://msdn.microsoft.com/en-us/library/ms187746.aspx[/url]):[quote][b]decimal and numeric (Transact-SQL)[/b][hr]p (precision) The maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point. The precision must be a value from 1 through the maximum precision of 38. [b]The default precision is 18.[/b][/quote]So when precision is not specified, SQL Server uses the value = 18.Another quote from BOL:[quote]s (scale) The maximum number of decimal digits that can be stored to the right of the decimal point. ... [b]The default scale is 0[/b]; therefore, 0 &amp;lt;= s &amp;lt;= p.[/quote]Thus the expression "CONVERT(DECIMAL, @var)" is equal to "CONVERT(DECIMAL(18,0), @var)".The batch from the example is equal to:[code="sql"]DECLARE @TestDecimal DECIMAL(8, 2)SET @TestDecimal = 275953.00SELECT CONVERT( DECIMAL(18,0), @TestDecimal * 0.40 ) ,    CONVERT( DECIMAL(18,0), @TestDecimal ) * 0.40[/code][quote][b]Bhavesh_Patel[/b][hr]Why answer differs when we are using the same function on same variable?[/quote]In the first expression, the result of multiplication is converted to DECIMAL:CONVERT(DECIMAL, @TestDecimal * 0.40) = CONVERT(DECIMAL(18,0), 275953.00 * 0.40) = CONVERT (DECIMAL(18,0), 110381.2000) = 110381In the second expression, the multiplier is converted to DECIMAL:CONVERT(DECIMAL, @TestDecimal) * 0.40 = CONVERT(DECIMAL(18,0), @TestDecimal) * 0.40 = 275953 * 0.40 = 110381.20</description><pubDate>Wed, 16 Dec 2009 05:04:18 GMT</pubDate><dc:creator>vk-kirov</dc:creator></item><item><title>RE: Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>Why answer differs when we are using the same function on same variable?</description><pubDate>Tue, 15 Dec 2009 23:17:07 GMT</pubDate><dc:creator>Bhavesh_Patel</dc:creator></item><item><title>Fun with Decimals</title><link>http://www.sqlservercentral.com/Forums/Topic834843-1717-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/68385/"&gt;Fun with Decimals&lt;/A&gt;[/B]</description><pubDate>Tue, 15 Dec 2009 22:24:22 GMT</pubDate><dc:creator>Julie Hargraves</dc:creator></item></channel></rss>