This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to get the list of Monday of an April 2018
Expected Output: Get the list of Monday of April 2018
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
DayDate ---------- 2018-04-02 DayDate ---------- 2018-04-09 DayDate ---------- 2018-04-16 DayDate ---------- 2018-04-23 DayDate ---------- 2018-04-30 |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Declare @Month Bigint Set @Month = 04 Declare @date date Set @date='2018-04-01' Declare @DayName VARCHAR(20) Set @DayName = 'Monday' while DATEPART(Month,@date)=@Month Begin DECLARE @Name VARCHAR(20) SELECT @Name = CASE ( DATEPART(dw, @Date) + @@DATEFIRST ) % 7 WHEN 1 THEN 'Sunday' WHEN 2 THEN 'Monday' WHEN 3 THEN 'Tuesday' WHEN 4 THEN 'Wednesday' WHEN 5 THEN 'Thursday' WHEN 6 THEN 'Friday' WHEN 0 THEN 'Saturday' END If @Name=@DayName Begin Select @date as DayDate End Set @date=DATEADD(Day,1,@date) End |
Please try the different solution for this puzzle and share it via comment...