Difference in month does not actually display last day of the month

When we want display a difference in months, datediff or date add does not help you in returning the last days of months, eg. if we do a dateadd(m,-3,'30 june 2005') will display '30 september 2005' but if you expected to get 1st of april 2005 then you got a problem,
the below function gives a work around for the problem.

DECLARE @Dt DATETIME
SET @Dt = '30 June 2004'
SET @Dt = DATEADD(m,-2,@Dt)
SET @Dt = DATEADD(d,-DATEPART(d,@dt)+1,@Dt)
SELECT @DT

Comments

Popular posts from this blog

Drupal - How to display webform node in a block?

Error when Installing SQL Server 2008 R2 Management Studio

Technical Team Lead Interview Questions