Technical Article

Get Number of Days in month

,

Have seen existing scripts to work out number of days in month. They tend to work out the month, and then whether it's a leap year.
Another approach is to take the date passed in, find the first day of the next month, and then use Datadd to take off one day, ie: last day of required month. Then Datepart to get the day number. Since it uses the servers datetime system, there is no need to worry about leap years as it will sort that out for you:

CREATE function funDaysInMonth
(@Date Datetime)
returns int
as
begin
declare @Days int
set @Date = (@Date - (day(@Date) - 1))
set @Date = dateadd(mm,1,@Date)
set @Date = dateadd(dd, -1, @Date)
set @Days = datepart(dd,@Date)
return (@Days)


end

Rate

4 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (1)

You rated this post out of 5. Change rating