我在Netezza SQL工作。
我的数据集中有周数和年份作为组合条目。例如," 2017年第3周和第34周。我想使用此字段来获取一周中最后一天的日期。在这种情况下,我应该在2017年1月21日"。
关于我该怎么做的任何想法?
感谢。
答案 0 :(得分:0)
这是一个应该适合你的例子:
SELECT DATE(year_num || '-01-01') + CAST(((week_num * 7) - 1) || ' DAYS' AS INTERVAL)
FROM (SELECT CAST(TRANSLATE(SUBSTR(dt, 0, INSTR(dt, ', ')), 'Week ', '') AS BYTEINT) AS week_num,
CAST(SUBSTR(dt, INSTR(dt, ', ') + 2) AS SMALLINT) AS year_num
FROM (SELECT 'Week 3, 2017' AS dt
UNION ALL
SELECT 'Week 19, 2015' AS dt
UNION ALL
SELECT 'Week 45, 2016' AS dt) _) _
答案 1 :(得分:0)
试试这个。
select to_date(lpad(trim(substring(data.dt,5,100)),8,'0'),'ww, YYYY') + interval '6 days' as lastDayOfWeek
from (SELECT 'Week 3, 2017' AS dt
UNION ALL
SELECT 'Week 19, 2015' AS dt
UNION ALL
SELECT 'Week 45, 2016' AS dt) data