Find Mondays between 2 dates

后端 未结 4 1666
梦如初夏
梦如初夏 2020-12-18 11:45

I need to display dates of all Mondays in the given date range.

For example, if my start date is 01/05/2015 and end date is 31/05/2015, I

4条回答
  •  轮回少年
    2020-12-18 12:25

    SET DATEFIRST 7; -- Set's sunday as first day of week, won't work otherwise
    
    DECLARE @StartDate DATE = '06/01/2015'
    
    DECLARE @EndDate DATETIME = '06/30/2015'
    
    DECLARE @TableOfDates TABLE(DateValue DATETIME)
    
    DECLARE @CurrentDate DATETIME
    
    SET @CurrentDate = @startDate
    
    WHILE @CurrentDate <= @endDate
    BEGIN
        INSERT INTO @TableOfDates(DateValue) VALUES (@CurrentDate)
    
        SET @CurrentDate = DATEADD(DAY, 1, @CurrentDate)
    END
    
    SELECT * FROM @TableOfDates WHERE DATEPART(weekday,Datevalue) = 2
    

提交回复
热议问题