Technical Article

Script to get the number of days in a month

,

Recently I saw here a script that returns the number of days of the month however I made my own script and I think that it's better elaborated. It's true that this is my opinion, but here it's. Evaluate it yourself.

print dbo.f_days_in_month('2000-02-21')

create function f_days_in_month (@date datetime) returns tinyint

   with encryption

as

begin

   declare @month tinyint, 
           @year  smallint

   select @month = datepart(mm,@date),
          @year  = datepart(yyyy,@date)

   return( case when @month in (1,3,5,7,8,10,12) then 31 
                when @month in (4,6,9,11) then 30 
                when @month = 2 and (@year % 4) = 0 then 29
                else 28
           end )
end

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating