SQL Query - Count number of Sundays between two dates

 declare  @StartDate DateTime = '2023-03-05',

         @EndDate DateTime = '2023-03-26';


WITH CTE_date AS (

      SELECT @StartDate as dt

      UNION ALL

      SELECT DATEADD(day,1,dt) as dt FROM CTE_date

      WHERE dt<=@EndDate

)

SELECT count(dt) as 'List of sundays'

FROM CTE_date

WHERE DATENAME(weekday ,dt) IN ('Sunday');


Post a Comment

Previous Post Next Post

Contact Form