Search This Blog

Tuesday, August 14, 2007

Get Number of Days in a Month

First Step is move to the next month using Dateadd with Month
SELECT DateAdd(MM, 1, GETDATE())

Next step is Extract the datepart
Day(DateAdd(MM, 1, GETDATE()))

Now if you subtract the value obtained in first step with the second step you will get the last day of previous month and if you again use day function for the same you will get the number of day for the month.

So the single query to retreive the same would be
Select Day(DateAdd(MM, 1, GETDATE()) - Day(DateAdd(MM, 1, GETDATE())))



No comments: