Posts

Showing posts from June, 2005

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