I would like to get (year of week number, week number) tuplets for a set of dates.
In Impala SQL, there is function weekofyear which gives the correct week number:
select year('2018-12-31'), weekofyear('2018-12-31')
year('2018-12-31')|weekofyear('2018-12-31')|
------------------|------------------------|
2018| 1|
But function year does not work if one wants to order (year, week number) tuplets in string format 'yyyyWwk'.
Is there a neat way to get 'yyyyWwk' for each date? Date 2018-12-31 given in the above example would yield result 2019W01.
This does not work:
select concat(cast(year(dt) as STRING),'W',lpad(cast(weekofyear(dt) as string),2,"0")) as wk, dt
from (
select '2018-11-30' as dt
union
select '2018-12-31' as dt
union
select '2019-01-31' as dt
) as t
order by 1, 2;
as it does not order the dates correctly:
wk |dt |
-------|----------|
2018W01|2018-12-31|
2018W48|2018-11-30|
2019W05|2019-01-31|
How to get a "week-year"=2019 for the date 2018-12-31?