Get Dates for the Current Month and Year eg: 01/11/2012 To 30/11/2012

Posted by Sourabh07 under Sql Server category on | Points: 40 | Views : 991
By inputting the value in @month and @year, the dates for the selected month and year will be displayed accordingly.....

Declare @month int

Declare @year int

set @month = 11
set @year = 2012

Declare @temp table(
[date] datetime

Declare @present_startdate datetime

set @present_startdate=CONVERT(datetime,'01/'+cast(@month as varchar)
+'/'+cast(@year as varchar),103)

Declare @previous_month_startdate datetime

set @previous_month_startdate = dateadd(M,1,@present_startdate)

while datediff(D,@present_startdate,@previous_month_startdate) <> 0

Insert into @temp values(@present_startdate)

set @present_startdate =DATEADD(D,1,@present_startdate)


select CONVERT(varchar,[date],103) from @temp

Comments or Responses

Login to post response