将日期转换为 ISO 周日期

Convert date to ISO week date

如何将 Impala SQL 中的日期转换为 ISO week date

例如 2019-12-30 在 ISO 星期日期日历中将写为 2020-W01-12020W011

答案:

将 Gordon Linoff 的答案标记为正确,因为它解决了问题的基本部分,即 ISO 周日期的年份部分的推导。

对于ISO周日期的星期部分,有一个ready函数,可以很容易地将ISO周日期的天部分从周日起始周转换为周一起始周。

下面的查询包含从星期一到星期日的所有星期日期:

select datecol, 
       concat(cast(iso_year as string),'-W',lpad(cast(iso_week as string),2,'0'),'-',cast(iso_day as string)) as iso_Year_week_date_long,
       concat(cast(iso_year as string),'W',lpad(cast(iso_week as string),2,'0'),cast(iso_day as string)) as iso_Year_week_date_short
  from (
SELECT datecol, 
       (case when weekofyear(datecol) = 1 and
                  date_part('year',datecol) <> date_part('year',adddate(datecol,+7))
             then date_part('year',datecol) + 1
             when weekofyear(datecol) in (52, 53) and
                  date_part('year',datecol) <> date_part('year',adddate(datecol,-7))
             then date_part('year',datecol) - 1
             else date_part('year',datecol)
         end) as iso_year,
       weekofyear(datecol) as iso_week,
       1+mod(dayofweek(datecol)+5,7) as iso_day
  from (
  select '2021-12-31' as datecol union
  select '2020-12-31' as datecol union
  select '2019-12-31' as datecol union
  select '2018-12-31' as datecol union
  select '2017-12-31' as datecol union
  select '2016-12-31' as datecol union
  select '2015-12-31' as datecol union
  select '2014-12-31' as datecol union
  select '2013-12-31' as datecol union
  select '2012-12-31' as datecol union
  select '2022-01-01' as datecol union
  select '2021-01-01' as datecol union
  select '2020-01-01' as datecol union
  select '2019-01-01' as datecol union
  select '2018-01-01' as datecol union
  select '2017-01-01' as datecol union
  select '2016-01-01' as datecol union
  select '2015-01-01' as datecol union
  select '2014-01-01' as datecol union
  select '2013-01-01' as datecol
 ) as t1
  ) as t2
order by datecol;

并显示 1 月 1 日属于

datecol   |iso_year_week_date_long|iso_year_week_date_short|
----------|-----------------------|------------------------|
2014-12-31|2015-W01-3             |2015W013                |
2015-01-01|2015-W01-4             |2015W014                |
2015-12-31|2015-W53-4             |2015W534                |
2016-01-01|2015-W53-5             |2015W535                |
2016-12-31|2016-W52-6             |2016W526                |
2017-01-01|2016-W52-7             |2016W527                |
2017-12-31|2017-W52-7             |2017W527                |
2018-01-01|2018-W01-1             |2018W011                |
2018-12-31|2019-W01-1             |2019W011                |
2019-01-01|2019-W01-2             |2019W012                |
2019-12-31|2020-W01-2             |2020W012                |
2020-01-01|2020-W01-3             |2020W013                |
2020-12-31|2020-W53-4             |2020W534                |
2021-01-01|2020-W53-5             |2020W535                |

认为 Impala returns date_part()extract() 的 iso 周 -- 基于你之前的问题。没有这方面的文档。

如果是这样,你可以使用条件逻辑:

select (case when date_part(week, datecol) = 1 and
                  date_part(year, datecol) <> date_part(year, datecol + interval 1 week)
             then date_part(year, datecol) + 1
             when date_part(week, datecol) in (52, 53) and
                  date_part(year, datecol) <> date_part(year, datecol - interval 1 week)
             then date_part(year, datecol) - 1
             else date_part(year, datecol)
         end) as iso_year,
        date_part(week, datecol) as iso_week

否则,您可以使用以下方法获取 iso 年份的第一天:

select (case when to_char('DD', date_trunc(year, datecol), 'DD') in ('THU', 'FRI', 'SAT', 'SUN')
             then next_day(date_trunc(year, date_trunc(year, datecol)), 'Monday')
             else next_day(date_trunc(year, date_trunc(year, datecol)), 'Monday') - interval 7 day
         end) as iso_year_start 

然后您可以使用算术从 is 年的开始计算 iso 周。

例如 ISO 星期日期日历中的 2019-12-30 将写为 2020-W01-1 或 2020W011。

我们可以使用字符串格式:

select cast(cast('2019-12-30' as date format 'YYYY-MM-DD') as string format 'iyyy-iw-id')

Returns:

"2020-01-01"