<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx</link><description>In other words, if we add a month, then subtract a month, we might not get back to the date we started from. For example: SELECT DATEADD ( MONTH , 1 , DATEADD ( MONTH , - 1 , '20100330' )) , DATEADD ( MONTH , - 1 , DATEADD ( MONTH , 1 , '20100330' ))</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx#31139</link><pubDate>Mon, 29 Nov 2010 23:09:27 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31139</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;I take it you just found such a bug. :-)&lt;/p&gt;
</description></item><item><title>re: Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx#31149</link><pubDate>Tue, 30 Nov 2010 02:13:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31149</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Aaron,&lt;/p&gt;
&lt;p&gt;I documented a feature, not found a bug. I am not claiming I was the first to document it ;)&lt;/p&gt;
</description></item><item><title>re: Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx#31154</link><pubDate>Tue, 30 Nov 2010 05:54:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31154</guid><dc:creator>Geri Reshef</dc:creator><description>&lt;p&gt;..And what do you think about the asociative feature:&lt;/p&gt;
&lt;p&gt;Select	2*(3/2),--=2&lt;/p&gt;
&lt;p&gt;	(2*3)/2;--=3&lt;/p&gt;
&lt;p&gt;:-)&lt;/p&gt;
&lt;p&gt;I Guess 28/02/2010 can be interpreted as:&lt;/p&gt;
&lt;p&gt;1. The last day of February.&lt;/p&gt;
&lt;p&gt;2. The 28th day of February.&lt;/p&gt;
</description></item><item><title>re: Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx#31156</link><pubDate>Tue, 30 Nov 2010 09:12:41 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31156</guid><dc:creator>Frank Kalis</dc:creator><description>&lt;p&gt;This happens for all months where the &amp;quot;previous&amp;quot; month has less days than the one you start with. Just try '20101231', for example.&lt;/p&gt;
&lt;p&gt;If you subtract a month from 30.03.2010 the result will be 28.02.2010 as there is no 30th day in February and the 28th happens to be the last day in February that year (will return 29th for any leap year). When you add a month again to 28.02.2010, you'll get 28.03.2010, because there is a 28th in every month and this makes it a perfectly valid date and result. It is maybe just not what you would expect if you were looking to get the last day of the month. &lt;/p&gt;
&lt;p&gt;On the other hand when you add a month to 30.03.2010 you'll get 30.04.2010, which is another perfectly valid date. Subtracting that month again, gives you the value you started with = 30.03.2010. Makes sense, I think.&lt;/p&gt;
&lt;p&gt;So, I guess this is no bug, but a feature. :-)&lt;/p&gt;
&lt;p&gt;Btw... doing integer arithmetics this&lt;/p&gt;
&lt;p&gt;Select 2*(3/2),--=2&lt;/p&gt;
&lt;p&gt;(2*3)/2;--=3&lt;/p&gt;
&lt;p&gt;returns exactly what I would expect.&lt;/p&gt;
</description></item><item><title>re: Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx#33165</link><pubDate>Thu, 03 Feb 2011 17:13:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:33165</guid><dc:creator>Luka Ramishvili</dc:creator><description>&lt;p&gt;It's not a bug, nor a feature, if you do that without computers and T-SQL, you will have the same result.&lt;/p&gt;
&lt;p&gt;For example, tell me in 31th of March, what day will be today minus one month, and then that day plus one month. &lt;/p&gt;
</description></item><item><title>re: Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx#33185</link><pubDate>Fri, 04 Feb 2011 02:52:06 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:33185</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Geri, Frank, Luka,&lt;/p&gt;
&lt;p&gt;Of course this behavior is not a bug. My point is different: when we assume any other behavior in our code then our code has a bug.&lt;/p&gt;
&lt;p&gt;When we need to be more precise, which is often the case in finances, we roll out our own end-of-month and day count conventions.&lt;/p&gt;
</description></item></channel></rss>