从 firebird 2.5 中的周数获取开始和结束日期
Get begin & end date from week number in firebird 2.5
我在一列中有周数,我想生成另外两个列来显示周开始日期和周结束日期。
我的table:
| week_nr | year_nr |
+---------+-----------+
| 2 | 2018 |
| 5 | 2018 |
我想生成这个table
| week_nr | year_nr | week_start | week_end |
+----------+-----------+-------------+-------------+
| 2 | 2018 | 08.01.2018 | 14.01.2018 |
| 5 | 2018 | 29.01.2018 | 04.02.2018 |
我尝试使用 "extract" 命令获取日期,但无法正常工作。
有谁知道 Firebird 2.5 的解决方案吗?
Firebird 中没有任何内置功能可以根据周数计算日期,您必须自己编写或找到一个现有的 UDF 来为您完成(例如 FreeAdhocUDF 中的 F_FIRSTDAYKW
)。
例如,使用 Firebird 3(!) 存储函数,您可以:
create function mondayOfIsoWeek(isoYear integer, isoWeek integer)
returns date
as
declare firstJanuary date;
declare firstThursday date;
declare firstWeek integer;
declare targetThursday date;
declare targetMonday date;
begin
firstJanuary = cast(isoYear || '-01-01' as date);
-- Thursday in the same week as the first of January
-- This is either in week 1 or the last week of the previous year
-- NOTE: 4 is Thursday
firstThursday = dateadd(day, 4 - extract(weekday from firstJanuary), firstJanuary);
firstWeek = extract(week from firstThursday);
-- Already in first week of year, compensate
if (firstWeek = 1) then
isoWeek = isoWeek - 1;
targetThursday = dateadd(week, isoWeek, firstThursday);
targetMonday = dateadd(day, -3, targetThursday);
return targetMonday;
end
Firebird 3 存储函数不能在 Firebird 2.5 中使用,但您可以将其用作存储过程的起点或执行您需要的执行块。如果绝对必要,您甚至可以将所有这些计算内联到一个表达式中(但这将非常不可读)。
我在一列中有周数,我想生成另外两个列来显示周开始日期和周结束日期。
我的table:
| week_nr | year_nr |
+---------+-----------+
| 2 | 2018 |
| 5 | 2018 |
我想生成这个table
| week_nr | year_nr | week_start | week_end |
+----------+-----------+-------------+-------------+
| 2 | 2018 | 08.01.2018 | 14.01.2018 |
| 5 | 2018 | 29.01.2018 | 04.02.2018 |
我尝试使用 "extract" 命令获取日期,但无法正常工作。 有谁知道 Firebird 2.5 的解决方案吗?
Firebird 中没有任何内置功能可以根据周数计算日期,您必须自己编写或找到一个现有的 UDF 来为您完成(例如 FreeAdhocUDF 中的 F_FIRSTDAYKW
)。
例如,使用 Firebird 3(!) 存储函数,您可以:
create function mondayOfIsoWeek(isoYear integer, isoWeek integer)
returns date
as
declare firstJanuary date;
declare firstThursday date;
declare firstWeek integer;
declare targetThursday date;
declare targetMonday date;
begin
firstJanuary = cast(isoYear || '-01-01' as date);
-- Thursday in the same week as the first of January
-- This is either in week 1 or the last week of the previous year
-- NOTE: 4 is Thursday
firstThursday = dateadd(day, 4 - extract(weekday from firstJanuary), firstJanuary);
firstWeek = extract(week from firstThursday);
-- Already in first week of year, compensate
if (firstWeek = 1) then
isoWeek = isoWeek - 1;
targetThursday = dateadd(week, isoWeek, firstThursday);
targetMonday = dateadd(day, -3, targetThursday);
return targetMonday;
end
Firebird 3 存储函数不能在 Firebird 2.5 中使用,但您可以将其用作存储过程的起点或执行您需要的执行块。如果绝对必要,您甚至可以将所有这些计算内联到一个表达式中(但这将非常不可读)。