SELECT Every3rdMondayDate = Date
FROM ( SELECT date
, ROW_NUMBER() OVER(PARTITION BY ym ORDER BY date DESC) AS RowNumber
FROM (
SELECT *
, LEFT(CONVERT(varchar, Date,112),6) ym
FROM (SELECT DATE
, (SELECT (DATENAME(dw,
CAST(DATEPART(m, DATE) AS VARCHAR)
+ '/'
+ CAST(DATEPART(d, DATE) AS VARCHAR)
+ '/'
+ CAST(DATEPART(yy, DATE) AS VARCHAR))
)) DayOfWeek
FROM (SELECT Row#
,CAST('20000101' AS DATETIME) + Row# [DATE]
FROM (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS Row#
FROM sys.all_columns) t1) t2) tbl1
WHERE DayOfWeek = 'Monday') tbl2
) tbl3
WHERE RowNumber = 3
If you find this script useful please donate generously.
No comments:
Post a Comment