Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the weekend dates of the current year in SQL?

I tried but could not get the right solution. I want an SQL query that lists all the weekend dates of the current year.

I tried this SQL query:

WITH hier(num, lvl) AS (
    SELECT  0, 1
            UNION ALL
    SELECT  100, 1
            UNION ALL 
    SELECT  num + 1, lvl + 1
    FROM    hier
    WHERE   lvl < 100 
)
SELECT lvl [Week], 
convert(date,DATEADD(dw, -DATEPART(dw, DATEADD(wk,DATEDIFF(wk,0,'12/31/'+convert(nvarchar,YEAR(getdate()))), 0)+6 ),
DATEADD(wk, DATEDIFF(wk,0,'12/31/'+convert(nvarchar,YEAR(getdate()))), 0)+6 ) - num  * 7,101) [End Date]
FROM    hier a
where   num < 52
ORDER BY [End Date] asc

Its output is like this:

Week  End date
52  2012-01-14
51  2012-01-21
50  2012-01-28
49  2012-02-04

I want the dates to start from the beginning – so, the above is missing one weekend, which is 2012-07-01. Also, I want the week numbers to show as 1, 2, 3... instead of 52, 51....

like image 710
Kartik Patel Avatar asked Sep 19 '12 06:09

Kartik Patel


People also ask

How do I find weekends in SQL?

MySQL WEEKDAY() Function The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.

How do I filter weekends in SQL?

For excluding weekend data we need to write the query as:SELECT * FROM table. WHERE ((DATEPART(dw, CheckedInDate) + @@DATEFIRST) % 7) NOT IN (0, 1)

How do I get the number of weekends of current month in SQL?

You Can simply use datediff function of sql. and then you can subtract weekends between those dates if any. For example check below query. And If You want to exclude holiday's too, then, You also can calculate holidays between start/end date and can subtract that from final selection.

How do I get weekday or weekend in SQL Server?

Take a look at the example: SELECT DATENAME(WEEKDAY, '2022-01-01' ); The result is 'Saturday'. There is also an alternative method to get a day name using the function FORMAT() .


1 Answers

Check out this blog post.

Your question is explained in detail.

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') -- For Weekend    
/*
WHERE DayName LIKE 'Sunday'
WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday
WHERE DayName LIKE 'Monday' -- For Monday
WHERE DayName LIKE 'Sunday' -- For Sunday
*/
OPTION (MaxRecursion 370)
like image 137
randoms Avatar answered Sep 23 '22 01:09

randoms