列出 SQL 中两个日期之间的所有工作日期
List all working dates between two dates in SQL
我有 SQL 代码来生成两个日期之间的日期列表,但我想从给定的两个日期生成工作日(工作日),
DECLARE @MinDate DATE = '20140101', @MaxDate DATE = '20140106';
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;
这是我的代码,所以任何人都请建议我单独获取工作日列表。
在线资源有代码来查找不列出所有日期的天数,这是我的困惑。
DECLARE @MinDate DATE = '20140101', @MaxDate DATE = '20140106';
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
WHERE datename(dw,@MinDate) NOT IN ('Saturday','Sunday')
AND datename(dw,@MaxDate) NOT IN ('Saturday','Sunday') ;
将原始查询设为 sub-select
,生成两个给定日期之间的所有日期,然后在 outer query
中进行过滤。
SET DATEFIRST 1
select [Date] from
(
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
)
where datename(dw,[Date]) not in ('Saturday','Sunday')
很久以前,我建立了一个日历 table 来回答像你这样的问题。除了易用性之外,主要的好处是您可以查看针对日历 table 的查询并说 "That's obviously right."
select cal_date, day_of_week
from calendar
where day_of_week in ('Mon', 'Tue', 'Wed', 'Thu', 'Fri')
and cal_date between '2014-01-01' and '2014-01-06'
order by cal_date;
cal_date day_of_week
--
2014-01-01 Wed
2014-01-02 Thu
2014-01-03 Fri
2014-01-06 Mon
我也有工作日的视图,所以我可以查询它。
select cal_date, day_of_week
from weekdays
where cal_date between '2014-01-01' and '2014-01-06'
order by cal_date;
试试这个:
DECLARE @MinDate DATE = '20140101',
@MaxDate DATE = '20140106'
;WITH N1 (N) AS (SELECT 1 FROM (VALUES (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) n (N)),
N2 (N) AS (SELECT 1 FROM N1 AS N1 CROSS JOIN N1 AS N2),
N3 (N) AS (SELECT 1 FROM N2 AS N1 CROSS JOIN N2 AS N2),
N4 (N) AS (SELECT ROW_NUMBER() OVER(ORDER BY N1.N) FROM N3 AS N1 CROSS JOIN N3 AS N2)
SELECT Date = DATEADD(DAY, N - 1, @MinDate)
FROM N4
WHERE
N < DATEDIFF(DAY, @MinDate, @MaxDate) + 2 AND
DATEDIFF(DAY, 1 - N, @MinDate) % 7 NOT IN (5,6)
结果:
Date
2014-01-01
2014-01-02
2014-01-03
2014-01-06
我有 SQL 代码来生成两个日期之间的日期列表,但我想从给定的两个日期生成工作日(工作日),
DECLARE @MinDate DATE = '20140101', @MaxDate DATE = '20140106';
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;
这是我的代码,所以任何人都请建议我单独获取工作日列表。 在线资源有代码来查找不列出所有日期的天数,这是我的困惑。
DECLARE @MinDate DATE = '20140101', @MaxDate DATE = '20140106';
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
WHERE datename(dw,@MinDate) NOT IN ('Saturday','Sunday')
AND datename(dw,@MaxDate) NOT IN ('Saturday','Sunday') ;
将原始查询设为 sub-select
,生成两个给定日期之间的所有日期,然后在 outer query
中进行过滤。
SET DATEFIRST 1
select [Date] from
(
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
)
where datename(dw,[Date]) not in ('Saturday','Sunday')
很久以前,我建立了一个日历 table 来回答像你这样的问题。除了易用性之外,主要的好处是您可以查看针对日历 table 的查询并说 "That's obviously right."
select cal_date, day_of_week
from calendar
where day_of_week in ('Mon', 'Tue', 'Wed', 'Thu', 'Fri')
and cal_date between '2014-01-01' and '2014-01-06'
order by cal_date;
cal_date day_of_week -- 2014-01-01 Wed 2014-01-02 Thu 2014-01-03 Fri 2014-01-06 Mon
我也有工作日的视图,所以我可以查询它。
select cal_date, day_of_week
from weekdays
where cal_date between '2014-01-01' and '2014-01-06'
order by cal_date;
试试这个:
DECLARE @MinDate DATE = '20140101',
@MaxDate DATE = '20140106'
;WITH N1 (N) AS (SELECT 1 FROM (VALUES (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) n (N)),
N2 (N) AS (SELECT 1 FROM N1 AS N1 CROSS JOIN N1 AS N2),
N3 (N) AS (SELECT 1 FROM N2 AS N1 CROSS JOIN N2 AS N2),
N4 (N) AS (SELECT ROW_NUMBER() OVER(ORDER BY N1.N) FROM N3 AS N1 CROSS JOIN N3 AS N2)
SELECT Date = DATEADD(DAY, N - 1, @MinDate)
FROM N4
WHERE
N < DATEDIFF(DAY, @MinDate, @MaxDate) + 2 AND
DATEDIFF(DAY, 1 - N, @MinDate) % 7 NOT IN (5,6)
结果:
Date
2014-01-01
2014-01-02
2014-01-03
2014-01-06