Search

Tuesday, July 12, 2011

Get First and Last Saturday of Each Month


declare @year int
set @year =2011
-- First and Last Sunday by SqlServerCurry.com
select min(dates) as first_saturday,max(dates) as last_saturday from
(select dateadd(day,number-1,DATEADD(year,@year-1900,0))
as dates from master..spt_values
where type='p' and number between 1 and
DATEDIFF(day,DATEADD(year,@year-1900,0),DATEADD(year,@year-1900+1,0))
) as t where DATENAME(weekday,dates)='saturday'
group by DATEADD(month,datediff(month,0,dates),0)

1 comment:

  1. Hi,
    This is excellent - exactly what I have been looking for....however....how could I return the date of the last Saturday of THIS month only?

    Thanks

    ReplyDelete