2017-08-15 52 views
1

我正在Netezza SQL中工作。在Aginity工作臺中從週數和年份獲取星期的最後一天

我已經將我的數據集中的星期數和年數作爲一個組合條目。例如,「2017年第3周」。我想使用此字段獲取一週中最後一天的日期。在這種情況下,我應該得到「2017年1月21日」。

有關我該怎麼做的任何想法?

謝謝。

回答

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) _) _ 
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 
相關問題