Wednesday, 25 June 2008

Number of days in month : Sql server

declare @month int, @year int, @date datetime
select @month = 2, @year = 2000
select @date = convert(varchar, @year) + right('0' + convert(varchar, @month), 2) + '01'
select datediff(d, @date, dateadd(m, 1, @date)) as "Number of Days"

-- This one determines the last day of the month.
-- Uses almost similar logic as the above statement.
select dateadd( d, -1 , dateadd( m , 1 , @date ) ) AS "Last Day Of Month"

kick it on DotNetKicks.com

2 comments:

  1. Very nice one yaar... It was very helpful. It is working even leap year..

    ReplyDelete