This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing one script to find a list of Weekends between given two Dates in SQL Server.
If you are a database developer and reading this post, you should know about the life of DBA.
Database Administrator life is for 24*7 and they have to also set few maintenance tasks over the weekends and holidays.
This script helps to DBA for finding available weekends between the dates so they can set auto-schedule for database maintenance.
Please access few related articles:
SQL Server 2012: Find First Sunday of Next Month using EOMONTH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
DECLARE @beginDate DATE = '20161201' DECLARE @endDate DATE = '20161231' DECLARE @Weekend TABLE ( Weekend DATE PRIMARY KEY ,IsWeekend BIT ) WHILE @beginDate <= @endDate BEGIN INSERT INTO @Weekend SELECT @beginDate AS Weekend ,(CASE WHEN DATEPART(WEEKDAY, @beginDate) In (7, 1) THEN 1 ELSE 0 END) AS IsWeekend SET @beginDate = DateAdd(Day, 1, @beginDate) END SELECT Weekend FROM @Weekend WHERE IsWeekend = 1 |
Leave a Reply