﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2012 / SQL Server 2012 -  T-SQL  / What's wrong in this .. simple calculations / 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>Mon, 20 May 2013 15:21:02 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>YOu may folow the money :hehe: by tracing intermediate results:[code="sql"]select (1400.05 / 4232.33 ), (1400.05 / 4232.33 ) * 4180.06, round((1400.05 / 4232.33 ) * 4180.06 ,2) --Statement 1declare @v1 smallmoney = 1400.05,@v2 smallmoney = 4232.33,@v3 smallmoney = 4180.06select @v1, (@v1 / @v2 ), (@v1 / @v2 ) * @v3, round((@v1 / @v2 ) * @v3 ,2) --Statement 2select @v1*1.0, (@v1*1.0 / @v2 ) ,(@v1*1.0 / @v2 ) * @v3 , round((@v1*1.0 / @v2 ) * @v3 ,2) --Statement 3[/code]For most accurate calculations always use FLOAT data type (unless you need to handle more than 15 digigts, than use "big" decimals).Try use FLOAT in your example and you'll see that 4 digit returned by DECIMAL calculation is incorrect.Fortunateluy for you, it's beyond rounding precision.</description><pubDate>Wed, 06 Mar 2013 17:12:03 GMT</pubDate><dc:creator>Sergiy</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>This behaviour is due to data type precedence.(@v1 / @v2 ) * @v3 will return smallmoney((@v1 * 1.0) / @v2 ) * @v3 will return numericThis can be demonstrated like this:[quote]DECLARE @v1 SMALLMONEY = 1400.05,		@v2 SMALLMONEY = 4232.33;SELECT @v1 / @v2 AS Val INTO TestTable1SELECT @v1 / @v2 * 1.0 AS Val INTO TestTable2EXEC sp_help TestTable1EXEC sp_help TestTable2DROP TABLE TestTable1DROP TABLE TestTable2[/quote]And you will see the following:[quote]TestTable1Name - ValType - smallmoneyLength - 4Precision - 10Scale - 4TestTable 2Name - ValType - numericLength - 9Precision - 13Scale - 5[/quote]</description><pubDate>Wed, 06 Mar 2013 03:42:58 GMT</pubDate><dc:creator>Sean Pearce</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>[quote][b]cphite (9/27/2012)[/b][hr]Speaking for myself, I always use DECIMAL to store dollar amounts to avoid exactly this sort of issue. I've never seen the point of money or smallmoney.[/quote]Backwards compatibility.</description><pubDate>Thu, 27 Sep 2012 12:21:53 GMT</pubDate><dc:creator>Evil Kraig F</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>[quote][b]diveshps (9/27/2012)[/b][hr]Laurie,Thank you for your reply.I guess if I force a cast to a decimal it will work since that is what happens when I add the "* 1.0" but I thought money or smallmoney was a decimal number.Even if I change the datatype to money, I get the same results.If we have to force a cast or conversion to a decimal type then does it mean that there is no point in using the money or smallmoney data type? !!! I hope this is not the conclusion of this. :-)Thanks for any more leads on this.RegardsDivesh[/quote]Speaking for myself, I always use DECIMAL to store dollar amounts to avoid exactly this sort of issue. I've never seen the point of money or smallmoney.</description><pubDate>Thu, 27 Sep 2012 11:34:20 GMT</pubDate><dc:creator>cphite</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>Discusses Issues with money.There are rounding issues with money and is probably not the best datatype to use in your scenario.[url=http://www.sqlservercentral.com/Forums/Topic544518-9-1.aspx][/url]</description><pubDate>Thu, 27 Sep 2012 11:13:33 GMT</pubDate><dc:creator>Ray M</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>Money &amp; Smallmoney both have 4 decimal places, so that's why they lose the detail.  If you're looking for accuracy it's better to use decimal.It's all a bit of a minefield.:hehe:</description><pubDate>Thu, 27 Sep 2012 11:05:52 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>Laurie,Thank you for your reply.I guess if I force a cast to a decimal it will work since that is what happens when I add the "* 1.0" but I thought money or smallmoney was a decimal number.Even if I change the datatype to money, I get the same results.If we have to force a cast or conversion to a decimal type then does it mean that there is no point in using the money or smallmoney data type? !!! I hope this is not the conclusion of this. :-)Thanks for any more leads on this.RegardsDivesh</description><pubDate>Thu, 27 Sep 2012 10:55:27 GMT</pubDate><dc:creator>diveshps</dc:creator></item><item><title>RE: What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>I think the intermediate result from @v1 / @v2 is being truncated because you're using smallmoney data type.Can you use a different datatype, or cast one value to decimal to make it clearer what's happening.e.g.[code="sql"]select round((CAST(@v1 as decimal(18,2)) / @v2 ) * @v3 ,2) --Statement 2[/code]or[code="sql"]declare @v1 smallmoney = 1400.05, @v2 smallmoney = 4232.33, @v3 smallmoney = 4180.06select round((CAST(@v1 as decimal(18,2)) / @v2 ) * @v3 ,2), --Statement 2	CAST(@v1 as decimal(18,2)) / @v2,	CAST(round((CAST(@v1 as decimal(18,2)) / @v2 ) * @v3 ,2) as SmallMoney) -- &amp;lt;----&amp;lt;&amp;lt;&amp;lt;[/code]</description><pubDate>Thu, 27 Sep 2012 10:40:22 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>What's wrong in this .. simple calculations</title><link>http://www.sqlservercentral.com/Forums/Topic1365374-3077-1.aspx</link><description>If I run the following on SQL Server 2012 I get two different answers from the first two select statments.Any reason why and what I need to do to avoid this. The second statement gives the wrong answer and that is the statement that I need to use in my code. With the change in statement 3 , I get the right answer but I am not getting the confidence that I should introduce the change in all my calculations.select round((1400.05 / 4232.33 ) * 4180.06 ,2) --Statement 1declare @v1 smallmoney = 1400.05,	@v2 smallmoney = 4232.33,	@v3 smallmoney = 4180.06select round((@v1 / @v2 ) * @v3 ,2) --Statement 2select round((@v1*1.0 / @v2 ) * @v3 ,2) --Statement 3Thank you very much for the help.RegardsDivesh</description><pubDate>Thu, 27 Sep 2012 10:23:24 GMT</pubDate><dc:creator>diveshps</dc:creator></item></channel></rss>