T-SQL how to get date range for 2 week pay period

前端 未结 2 1572
孤独总比滥情好
孤独总比滥情好 2021-01-21 08:55

I have following seed input data:

  1. Payperiod start: Dec 3rd
  2. Payperiod end: Dec 16

Thats just example how company payroll goes.

Now,

2条回答
  •  清歌不尽
    2021-01-21 09:50

    Would the following not work?

    DECLARE @StartDate DATETIME
    SELECT @StartDate = '2016-05-01 00:00:00.000'
    DECLARE @EndDate DATETIME
    SELECT @EndDate = '2017-04-01 00:00:00.000'
    
    DECLARE @Dates TABLE(StartDate DATETIME, EndDate DATETIME)
    
    WHILE @StartDate <= @EndDate
    BEGIN
    INSERT INTO @Dates (StartDate, EndDate)
    SELECT @StartDate - 13, @StartDate WHERE DATEPART(WEEK, @StartDate) % 2 = 1 AND DATEPART(WEEKDAY, @StartDate) = 7
    
    SET @StartDate = @StartDate + 1 
    END
    
    -- Check the dates here... should be every 2 weeks
    SELECT * FROM @Dates
    

提交回复
热议问题