Get a certain day of the week between a range of dates in SQL [closed]

2

How to Get the dates of a certain day of the week between two dates. Example: All the dates of the Monday days between the range of 2016-01-19 to 2016-02-19 Reputed: 2016-01-23 2016-01-30 2016-02-06 2016-02-13

This is what I am doing but I am getting on Wednesdays:

DECLARE @TEMP TABLE(REGISTRO DATE)
DECLARE @startdate datetime = '2016-01-19'
DECLARE @enddate datetime = '2016-02-19' ;with cte(col)
AS
(
SELECT @startdate
UNION ALL
SELECT col + 1
FROM cte
WHERE col <= @enddate
)
INSERT INTO @TEMP
SELECT *
FROM cte
WHERE DATEDIFF(dd,0,col)% 7 = 0

SELECT * FROM @TEMP
    
asked by Danitza Ayala 19.01.2017 в 16:47
source

1 answer

0

Use the DATEPART function to filter by the day of the week of the date.

I think the day depends on the configuration of the server; in my case on Monday returns 1, but do tests for your particular case.

SELECT @startdate
UNION ALL
SELECT col + 1
FROM cte
WHERE col <= @enddate
)
INSERT INTO @TEMP
SELECT *
FROM cte
WHERE DATEPART(weekday,col) = 1;
    
answered by 19.01.2017 в 17:08