1
我正在嘗試編寫sql查詢來查找過去的第3個工作日,不包括假期和週末。以下是我的方法,但它沒有給出正確的解決方案。請建議SQL Query查找過去的第3個工作日,不包括節假日和週末
第一種方法:
alter function getdate(@givendate datetime)
returns datetime
AS
BEGIN
declare @calcdate datetime
declare @reqdate datetime
declare @count bit
set @count=1
select @[email protected];
select @[email protected];
while(@calcdate<@givendate)
begin
while(@count=1)
begin
if exists(select 1 from dbo.holidays with(Nolock) where [email protected])
begin
select @[email protected]
end
else
begin
set @count=0
end
end
select @[email protected]+1
end
return @reqdate
END
第二種方法:
alter function dbo.getpast3day (@date date)
returns datetime
As
begin
declare @reqdate datetime;
declare @Newyears_day datetime;
declare @Martin_Luther_Kin_Birthday datetime;
declare @presidents_day datetime;
declare @Memorial_day datetime;
declare @Independence_day datetime;
declare @Labour_day datetime;
declare @Columbus_day datetime;
declare @Veterans_day datetime;
declare @Thanksgiving_day datetime;
declare @Christmas_day datetime;
SELECT @reqdate= DATEADD(DAY, CASE (DATEPART(WEEKDAY, @date) + @@DATEFIRST) % 7
WHEN 1 THEN -5
WHEN 2 THEN -6
WHEN 3 THEN -6
WHEN 4 THEN -6
WHEN 5 THEN -6
ELSE -4
END, DATEDIFF(DAY, 0, @date)) ;
[email protected]_Luther_Kin_Birthday
begin
WITH dates AS (
SELECT DATEADD(day, number, @date) AS theDate
FROM master.dbo.spt_values
WHERE type = 'P'
)
SELECT @Martin_Luther_Kin_Birthday=theDate
FROM dates
WHERE DATEDIFF(day, '19000101', theDate) % 7 = 0
AND DATEPART(day, thedate)>=15 and DATEPART(day, thedate)<=21 and DATEPART(month,thedate)=1 and datepart(year,thedate)= year(@date);
end
[email protected]_day
begin
WITH dates AS (
SELECT DATEADD(day, number, @date) AS theDate
FROM master.dbo.spt_values
WHERE type = 'P'
)
SELECT @presidents_day=theDate
FROM dates
WHERE DATEDIFF(day, '19000101', theDate) % 7 = 0
AND DATEPART(day, thedate)>=15 and DATEPART(day, thedate)<=21 and DATEPART(month,thedate)=2 and datepart(year,thedate)= year(@date);
end
[email protected]_day
begin
WITH dates AS (
SELECT DATEADD(day, number, @date) AS theDate
FROM master.dbo.spt_values
WHERE type = 'P'
)
SELECT @Memorial_day=theDate
FROM dates
WHERE DATEDIFF(day, '19000101', theDate) % 7 = 0
AND DATEPART(day, thedate)>=22 and DATEPART(day, thedate)<=31 and DATEPART(month,thedate)=2 and datepart(year,thedate)= year(@date);
end
[email protected]_day
begin
WITH dates AS (
SELECT DATEADD(day, number, @date) AS theDate
FROM master.dbo.spt_values
WHERE type = 'P'
)
SELECT @Labour_day=theDate
FROM dates
WHERE DATEDIFF(day, '19000101', theDate) % 7 = 0
AND DATEPART(day, thedate)>=1 and DATEPART(day, thedate)<=7 and DATEPART(month,thedate)=9 and datepart(year,thedate)= year(@date);
end
[email protected]_day
begin
WITH dates AS (
SELECT DATEADD(day, number, @date) AS theDate
FROM master.dbo.spt_values
WHERE type = 'P'
)
SELECT @Columbus_day=theDate
FROM dates
WHERE DATEDIFF(day, '19000101', theDate) % 7 = 0
AND DATEPART(day, thedate)>=8 and DATEPART(day, thedate)<=14 and DATEPART(month,thedate)=10 and datepart(year,thedate)= year(@date);
end
[email protected]_day
begin
WITH dates AS (
SELECT DATEADD(day, number, @date) AS theDate
FROM master.dbo.spt_values
WHERE type = 'P'
)
SELECT @Thanksgiving_day=theDate
FROM dates
WHERE DATEDIFF(day, '19000101', theDate) % 7 = 0
AND DATEPART(day, thedate)>=22 and DATEPART(day, thedate)<=31 and DATEPART(month,thedate)=11 and datepart(year,thedate)= year(@date);
end
if (@[email protected]Martin_Luther_Kin_Birthday or @[email protected]_day or @[email protected]_day or @[email protected]_day or @[email protected]_day or @[email protected]_day)
begin
select @reqdate= DATEADD(day,-6,@reqdate)
end
--RETURN '2216-09-20 00:00:00.000'
return @reqdate
end