Skip to content
Advertisement

Get all dates of a Year based on Day Name [closed]

I want to take all Dates of an year based on Day Name. For example If I pass Thu/Thursday and 2020 (Year). I need to get all the Dates which are coming under Thursday on 2020. I might sent one or more Day names For example I might sent Thursday, Friday. I couldn’t find any answer to this.

Advertisement

Answer

I would use a recursive CTE to generate all the dates and then filter the ones you want:

with dates as (
      select datefromparts(2020, 1, 1) as dte
      union all
      select dateadd(day, 1, dte)
      from dates
      where dte < datefromparts(2020, 12, 31)
     )
select d.*
from dates d
where datename(weekday, dte) = 'Thursday'
option (maxrecursion 0);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement