Wednesday, April 2, 2008

SQL SERVER – Get Date of All Weekends of the Year

Beginning SQL Server 2005 Programming (Programmer to Programmer)

how to generate the date for all the Sundays in the upcoming year?

DECLARE @Year AS INT,

@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME


-- You can change @year to any year you desire


SELECT @year = 2010
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)


-- Creating Query to Prepare Year Data


;WITH cte AS (
SELECT 1 AS DayID,
@FirstDateOfYear AS FromDate,
DATENAME(dw, @FirstDateOfYear) AS Dayname
UNION ALL
SELECT cte.DayID + 1 AS DayID,
DATEADD(d, 1 ,cte.FromDate),
DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
FROM cte
WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear


)
SELECT FromDate AS Date, Dayname
FROM CTE
WHERE DayName IN ('Saturday','Sunday')
/*
WHERE DayName IN ('Saturday,Sunday') -- For Weekend
WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday
WHERE DayName LIKE 'Monday' -- For Monday
WHERE DayName LIKE 'Sunday' -- For Sunday
*/
OPTION (MaxRecursion 370)

The result will be dates along with days in next column as expected.

Inside Microsoft SQL Server 2005: T-SQL Querying (Solid Quality Learning)Microsoft SQL Server Standard Edition 2005 32 Bit CD/DVD 5 Client

No comments:

Post a Comment