简体   繁体   English

如何在SQL Server中获得每月每月两次的双周日期?

[英]How to get biweekly dates per month in a year in SQL Server?

I am trying to generate biweekly dates when I set a start year and end year. 当我设置开始年和结束年时,我试图生成每两周一次的日期。

DECLARE @StartYear DATETIME
DECLARE @EndYear DATETIME

SET @StartYear = '01/01/2017'
SET @EndYear = '12/31/2017'

The T-SQL should then compute the biweekly dates, example: 03/15/2017 and 03/30/2017 are the paydays. 然后,T-SQL应该计算两周一次的日期,例如:03/15/2017和03/30/2017是发薪日。

Also if the dates fall on Saturday and Sunday, then it will generate the dates to its 1st Friday. 同样,如果日期位于星期六和星期日,则它将生成到其第一个星期五的日期。

Hope someone could help me with this. 希望有人可以帮助我。 Or someone would like to share his/her knowledge and formula about this. 或者有人想分享他/她的知识和公式。

UPDATE : my expected result as follows: 更新 :我的预期结果如下:

when I enter 01/01/2017, then it will automatically generates every 15th day. 当我输入01/01/2017时,它将每15天自动生成一次。

Example: 例:

@StartYear: 01/01/2017

Result should be: 结果应为:

DatesBiweeklyPerMonthInAYear
-------------------------------
01/13/2017 (since the 15th day falls on sunday)
01/30/2017
02/15/2017
02/28/2017 (since no 30th day)

If 15th day falls on sun or sat it will fall on Friday of that specific week. 如果第15天落在太阳上或坐在其上,则将落在该特定星期的星期五。 so on... until the end of the year. 以此类推...直到年底。

Is this achievable? 这可以实现吗? Or not? 或不?

Thanks! 谢谢!

Here is a start. 这是一个开始。 you will have to tweak the logic to get your desired results. 您将需要调整逻辑以获得所需的结果。

DECLARE @MinDate DATE = '20170101', 
        @MaxDate DATE = '20171231'; 
DECLARE @date DATE; 
DECLARE @counter INT = 0; 
DECLARE my_cursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR 
  SELECT TOP (Datediff(day, @MinDate, @MaxDate) + 1) Date = 
  Dateadd(day, Row_number() 
                 OVER( 
                   ORDER 
  BY a.object_id) - 1, @MinDate) 
  FROM   sys.all_objects a 
         CROSS JOIN sys.all_objects b; 

OPEN my_cursor 

FETCH next FROM my_cursor INTO @date 

WHILE @@FETCH_STATUS = 0 
  BEGIN 
      IF( @counter = 15 ) 
        BEGIN 
            PRINT @date -- here is where you get the 15th date
            SET @counter = 0 
        END 
      SET @counter = @counter + 1 
      FETCH next FROM my_cursor INTO @date 
  END 

CLOSE my_cursor 

DEALLOCATE my_cursor 

your business rule is not 100% clear. 您的业​​务规则并非100%明确。

I think there can be more than one correct result. 我认为可以有不止一个正确的结果。

In one place it is hard coded,because i want to be 100% sure of requirement. 在一个地方很难编码,因为我想100%确定要求。 I am not using cursor.Its only 24 loops. 我没有使用游标,它只有24个循环。

declare @StartYear datetime='2017-01-01'
declare @endYear datetime ='2017-12-31'

declare @gap int =14 --Bimonthly means gap of 14 days or 15 days whatever

;With CTE as
(
select dateadd(day,@gap, @StartYear) Bimonthly
,1 rn

UNION ALL

select  
case 

when (rn+1)%2=0 and datename(m, Bimonthly)='February' THEN
      '2017-02-28'
      when (rn+1)%2=0 and datename(m, Bimonthly)!='February' 

then dateadd(day,@gap, Bimonthly)
else 
dateadd(day,@gap, dateadd(month, datediff(month,0,dateadd(month,1,Bimonthly)),0))
END
,rn+1
from cte
where rn< (datediff(month,@StartYear, @endYear)+1)*2

)
select 
case WHEN datename(dw, Bimonthly)='Saturday' THEN
      dateadd(day,-1, Bimonthly) 
      WHEN datename(dw, Bimonthly)='Sunday' THEN 
      dateadd(day,-2, Bimonthly) 
     else 
      Bimonthly
     end 

     ,rn

from cte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM