计算 2 个日期之间的 INCLUDING 周数
Calculate Number of INCLUDING Weeks between 2 dates
我需要计算 INCLUDING 周数(即使下周只有 1 天(周从星期一开始,在星期日结束), 也算作一周) 两个日期之间。
如果我使用常规的 datediff,两者的输出都是 2。
SELECT datediff(ww, '2022-03-01','2022-03-13')
SELECT datediff(ww, '2022-03-14','2022-03-31')
但我需要获得第 2 周和第 3 周:
From Date
To Date
Expected
2022-03-01
2022-03-13
falls in 2 weeks (02/28/2022 - 03/06/2022 and 03/07/2022 - 03/13/2022)
2022-03-14
2022-03-31
falls in 3 weeks (03/14/2022 - 03/20/2022, 03/21/2022 - 03/27/2022 and 03/28/2022 - 04/03/2022)
这是一个解决方案。
注意 SQL 服务器将星期日作为一周的第一天。如果我们希望将星期一算作一周的第一天,我们必须从我们的日期中删除一天,如第二个查询。
CREATE TABLE dates (
From_Date date,
To_Date date);
INSERT INTO dates VALUES
('2022-03-01','2022-03-13'),
('2022-03-14','2022-03-31');
SELECT
From_Date ,
DATENAME(ww, From_Date) FromWeek,
To_Date,
DATENAME(ww, To_Date) ToWeek,
1 + CAST( DATENAME(ww, To_Date) AS INT)
- CAST( DATENAME(ww, From_Date) AS INT) WeekDiff
FROM dates;
From_Date | FromWeek | To_Date | ToWeek | WeekDiff
:--------- | :------- | :--------- | :----- | -------:
2022-03-01 | 10 | 2022-03-13 | 12 | 3
2022-03-14 | 12 | 2022-03-31 | 14 | 3
SELECT
From_Date ,
DATENAME(ww, From_Date) FromWeek,
To_Date,
DATENAME(ww, To_Date) ToWeek,
1 + CAST( DATENAME(ww, DATEADD(day, -1, To_Date)) AS INT)
- CAST( DATENAME(ww, DATEADD(day, -1, From_Date)) AS INT) WeekDiff
FROM dates;
From_Date | FromWeek | To_Date | ToWeek | WeekDiff
:--------- | :------- | :--------- | :----- | -------:
2022-03-01 | 10 | 2022-03-13 | 12 | 2
2022-03-14 | 12 | 2022-03-31 | 14 | 3
db<>fiddle here
我需要计算 INCLUDING 周数(即使下周只有 1 天(周从星期一开始,在星期日结束), 也算作一周) 两个日期之间。
如果我使用常规的 datediff,两者的输出都是 2。
SELECT datediff(ww, '2022-03-01','2022-03-13')
SELECT datediff(ww, '2022-03-14','2022-03-31')
但我需要获得第 2 周和第 3 周:
From Date | To Date | Expected |
---|---|---|
2022-03-01 | 2022-03-13 | falls in 2 weeks (02/28/2022 - 03/06/2022 and 03/07/2022 - 03/13/2022) |
2022-03-14 | 2022-03-31 | falls in 3 weeks (03/14/2022 - 03/20/2022, 03/21/2022 - 03/27/2022 and 03/28/2022 - 04/03/2022) |
这是一个解决方案。 注意 SQL 服务器将星期日作为一周的第一天。如果我们希望将星期一算作一周的第一天,我们必须从我们的日期中删除一天,如第二个查询。
CREATE TABLE dates ( From_Date date, To_Date date); INSERT INTO dates VALUES ('2022-03-01','2022-03-13'), ('2022-03-14','2022-03-31');
SELECT From_Date , DATENAME(ww, From_Date) FromWeek, To_Date, DATENAME(ww, To_Date) ToWeek, 1 + CAST( DATENAME(ww, To_Date) AS INT) - CAST( DATENAME(ww, From_Date) AS INT) WeekDiff FROM dates;
From_Date | FromWeek | To_Date | ToWeek | WeekDiff :--------- | :------- | :--------- | :----- | -------: 2022-03-01 | 10 | 2022-03-13 | 12 | 3 2022-03-14 | 12 | 2022-03-31 | 14 | 3
SELECT From_Date , DATENAME(ww, From_Date) FromWeek, To_Date, DATENAME(ww, To_Date) ToWeek, 1 + CAST( DATENAME(ww, DATEADD(day, -1, To_Date)) AS INT) - CAST( DATENAME(ww, DATEADD(day, -1, From_Date)) AS INT) WeekDiff FROM dates;
From_Date | FromWeek | To_Date | ToWeek | WeekDiff :--------- | :------- | :--------- | :----- | -------: 2022-03-01 | 10 | 2022-03-13 | 12 | 2 2022-03-14 | 12 | 2022-03-31 | 14 | 3
db<>fiddle here