This happens for all months where the "previous" month has less days than the one you start with. Just try '20101231', for example.

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.

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.

Leave a Comment

About Alexander Kuznetsov

Alex Kuznetsov has been working with object oriented languages, mostly C# and C++, as well as with databases for more than a decade. He has worked with Sybase, SQL Server, Oracle and DB2. He regularly blogs on sqlblog.com, mostly about database unit testing, defensive programming, and query optimization.
Alex has written a book entitled "Defensive Database Programming with Transact-SQL" and several articles on simple-talk.com and devx.com. Currently he works as an agile developer.