这将在“where”条款中......如何找到基于工资单周期的季度?我们的工资周期从本季度的25日开始,到季度的第10个月结束。例如... 第二季度从2016年4月25日开始,到2016年10月7日结束。
答案 0 :(得分:0)
好的,很难完全理解你在做什么,但这里应该有用。您可以编辑日期以适合您的需要,但我根据您的说法制作了日期。
--This is just test data... you can use your own table
IF OBJECT_ID('tempdb..#payroll') IS NOT NULL DROP TABLE #payroll
CREATE TABLE #payroll (dates date)
INSERT INTO #payroll (dates) VALUES
('1/1/2016'),
('1/16/2016'),
('2/4/2016'),
('3/3/2016'),
('3/19/2016'),
('4/18/2016'),
('5/6/2016'),
('6/4/2016'),
('6/29/2016'),
('7/4/2016'),
('7/31/2016'),
('8/9/2016'),
('9/1/2016'),
('10/3/2016'),
('10/19/0216'),
('11/4/2016'),
('11/21/2016'),
('12/2/2016'),
('1/1/2016'),
('1/8/2017'),
('1/21/2017')
--variable for what ever quarter you want to limit on. 1-4
declare @Quarter int
set @Quarter = 4
--Year that you are focused on. If left to NULL it uses the current year
declare @Year int
set @Year = NULL
IF @Year IS NULL
BEGIN
SET @Year = Year(GETDATE())
END
--Date parameters that we will use to filter
declare @startDate date = null
declare @endDate date = null
--logic to set your quarters
set @startDate = case
when @Quarter = 1 then '1/25/' + CAST(@Year as varchar(4))
when @Quarter = 2 then '4/25/' + CAST(@Year as varchar(4))
when @Quarter = 3 then '7/25/' + CAST(@Year as varchar(4))
when @Quarter = 4 then '9/25/' + CAST(@Year as varchar(4))
end
set @endDate = case
when @Quarter = 1 then '4/10/' + CAST(@Year as varchar(4))
when @Quarter = 2 then '7/10/' + CAST(@Year as varchar(4))
when @Quarter = 3 then '9/10/' + CAST(@Year as varchar(4))
when @Quarter = 4 then '1/10/' + CAST((@Year + 1)as varchar(4))
end
--run it to test results
select *
from #payroll
where dates between @startDate and @endDate