Wednesday, September 22, 2010

Stored procedure for finding number days in month

Finding number of days in a month

alter procedure no_of_days as
Declare @no_of_days int
Declare @prev_month varchar(100)
Declare @current_month varchar(100)
Declare @file_duration varchar(100)
DECLARE @returnDate INT
declare @days int

SET @returnDate = CASE WHEN MONTH(getdate())
IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(getdate()) IN (4, 6, 9, 11) THEN 30
ELSE CASE WHEN (YEAR(getdate()) % 4 = 0
AND
YEAR(getdate()) % 100 != 0)
OR
(YEAR(getdate()) % 400 = 0)
THEN 29
ELSE 28 END
END

Get previous month name and current month name

set @no_of_days=@returnDate
set @prev_month=(Select Datename(mm, GetDate()-@no_of_days))
set @current_month=(Select Datename(mm, GetDate()))
set @file_duration=@prev_month+'-'+@current_month

No comments: