0

I am almost a newbie to writing SQL queries. In the context of SQL Server, how to get list of 2nd and 4th Saturday dates in the year 2016?

3
  • 5
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Nov 13, 2015 at 14:16
  • How come you want to solve that specific problem? School assignment?
    – jarlh
    Nov 13, 2015 at 14:18
  • one of the use-cases we had encountered Nov 17, 2015 at 6:19

3 Answers 3

6

Done as a derived table simply to show the logic but you can reduce if you prefer:

select *
from (
    select d2016, 
           datename( weekday, d2016 ) as wkdy,
           row_number( ) over ( partition by datepart( month, d2016 ), datename( weekday, d2016 ) order by d2016 ) as rn_dy_mth
    from (
        select dateadd( day, rn, cast( '2016-01-01' as date ) ) as d2016
        from (
            select row_number() over( order by object_id ) - 1 as rn
            from sys.columns
            ) as rn
        ) as dy
    ) as dy_mth
where rn_dy_mth in ( 2, 4 )
  and wkdy = 'Saturday' 
order by d2016
2
0
--DEFINE LIMITS FOR DAY
DECLARE @TODATE DATETIME, @FROMDATE DATETIME
SET @FROMDATE ='2010-01-01'
SET @TODATE = '2017-12-31'

;WITH DATESEQUENCE( [DATE] ) AS
(
    SELECT @FROMDATE AS [DATE]
        UNION ALL
    SELECT DATEADD(DAY, 1, [DATE])
        FROM DATESEQUENCE
        WHERE DATE < @TODATE
    )
, DATESATURDAY AS
(SELECT CAST(CAST(YEAR([DATE]) AS VARCHAR)+ 
       (CASE WHEN DATEPART(M,[DATE])<=9 THEN '0'+CAST(DATEPART(M,[DATE]) AS VARCHAR)
       ELSE CAST(DATEPART(M,[DATE]) AS VARCHAR) END ) AS NUMERIC)  AS MONTH_ID

        ,CONVERT(VARCHAR,[DATE],106) AS DAY_DESC

       ,UPPER(DATENAME(DW,[DATE]))AS DAY_NAME
FROM DATESEQUENCE )
,SECOND_FOURTH_SATURDAY AS
(SELECT *
,ROW_NUMBER() OVER (PARTITION BY MONTH_ID ORDER BY DAY_NAME) FALL_IN
FROM DATESATURDAY
WHERE DAY_NAME='SATURDAY')
SELECT * FROM SECOND_FOURTH_SATURDAY
WHERE FALL_IN IN(2,4)
OPTION (MAXRECURSION 10000)
1
  • While your post may answer the question it wouldn't hurt explaining it a little. Nov 1, 2017 at 12:03
0

You can get any Saturday of a month using the Following Query in SQL. Here I'm Getting on Current Date, You can set your own selected date to get a Specific month Saturday

select DATEADD(dd, (14 - @@DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0,getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0)) as FirstSaturday,
DATEADD(dd,7,DATEADD(dd, (14 - @@DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) as SecondSaturday,
DATEADD(dd,14,DATEADD(dd, (14 - @@DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) as ThirdSaturday,
DATEADD(dd,21,DATEADD(dd, (14 - @@DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) as LastSaturday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.