T-SQL如何获得为期两周的薪资范围

时间:2021-12-28 22:09:48

I have following seed input data:

我有以下种子输入数据:

  1. Payperiod start: Dec 3rd
  2. Payperiod开始:12月3日
  3. Payperiod end: Dec 16
  4. Payperiod结束:12月16日

Thats just example how company payroll goes.

这只是公司工资的例子。

Now, I have just 1 date input, for example 12/30 (as today) I need to output date range for current pay period which will be: Dec 17 to Dec 30

现在,我只有一个日期输入,例如12/30(如今天),我需要输出当前支付周期的日期范围,即:12月17日至12月30日

And, same thing if I input Jan 3 - I should get back Dec 31 to Jan 13 2012

同样,如果我输入1月3日-我应该回到2012年12月31日到1月13日

Is there any shortcut in T-SQL or I have to do loops?

在T-SQL中有没有捷径,或者我必须做循环?

EDIT: To summarize question. if we know when pay cycle starts (in past) - how do I figure pay period start for given date?

编辑:总结问题。如果我们知道工资周期何时开始(过去)——我如何计算给定日期的工资周期开始?

2 个解决方案

#1


5  

You need some modulo operations and DATEDIFF.

您需要一些模块操作和DATEDIFF。

declare @periodStart datetime
declare @periodEnd datetime

set @periodStart = CAST('2011-12-03' as datetime)
set @periodEnd = CAST('2011-12-16' as datetime)

declare @anyDate datetime
set @anyDate = CAST('2011-12-30' as datetime)

declare @periodLength int
set @periodLength = DATEDIFF(day, @periodStart, @periodEnd) + 1


declare @daysFromFirstPeriod int
set @daysFromFirstPeriod = DATEDIFF(day, @periodStart, @anyDate)
declare @daysIntoPeriod int
set @daysIntoPeriod = @daysFromFirstPeriod % @periodLength

select @periodLength as periodLength, @daysFromFirstPeriod as daysFromFirstPeriod, @daysIntoPeriod as daysIntoPeriod
select DATEADD(day, -@daysIntoPeriod, @anyDate) as currentPeriodStart, DATEADD(day, @periodLength -@daysIntoPeriod, @anyDate) as currentPeriodEnd

Gives output

给出了输出

periodLength    daysFromFirstPeriod daysIntoPeriod
14              27                  13

and

currentPeriodStart        currentPeriodEnd
2011-12-17 00:00:00.000   2011-12-31 00:00:00.000

#2


0  

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

#1


5  

You need some modulo operations and DATEDIFF.

您需要一些模块操作和DATEDIFF。

declare @periodStart datetime
declare @periodEnd datetime

set @periodStart = CAST('2011-12-03' as datetime)
set @periodEnd = CAST('2011-12-16' as datetime)

declare @anyDate datetime
set @anyDate = CAST('2011-12-30' as datetime)

declare @periodLength int
set @periodLength = DATEDIFF(day, @periodStart, @periodEnd) + 1


declare @daysFromFirstPeriod int
set @daysFromFirstPeriod = DATEDIFF(day, @periodStart, @anyDate)
declare @daysIntoPeriod int
set @daysIntoPeriod = @daysFromFirstPeriod % @periodLength

select @periodLength as periodLength, @daysFromFirstPeriod as daysFromFirstPeriod, @daysIntoPeriod as daysIntoPeriod
select DATEADD(day, -@daysIntoPeriod, @anyDate) as currentPeriodStart, DATEADD(day, @periodLength -@daysIntoPeriod, @anyDate) as currentPeriodEnd

Gives output

给出了输出

periodLength    daysFromFirstPeriod daysIntoPeriod
14              27                  13

and

currentPeriodStart        currentPeriodEnd
2011-12-17 00:00:00.000   2011-12-31 00:00:00.000

#2


0  

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