Skip to content
Advertisement

Convert date to ISO week date

How to convert dates to ISO week date in Impala SQL?

For example 2019-12-30 in the ISO week date calendar would be written as 2020-W01-1 or 2020W011

ANSWER:

Marked Gordon Linoff answer as correct, as it solves the essential part of the question, the deducing of the year part of ISO week date.

For the week part of the ISO week date there is a ready function, and the day part of the ISO week date can be easily converted from Sunday starting week to Monday starting week.

The query below contains all week dates from Monday to Sunday:

and shows how January 1st belongs to

  • the new year, if January 1st is 1st, 2nd, 3rd or 4th day of the week, i.e., if there are at least 4 new year days in the week containing January 1st
  • the old year, if January 1st is 5th, 6th or 7th day of the week, i.e., if there are 3 or less new year days in the week containing January 1st

Advertisement

Answer

I think Impala returns the iso week for date_part() and extract() — based on your previous question. There is no documentation to this effect.

If so, you can use conditional logic:

Otherwise, you can get the first day of the iso year using:

You can then calculate the iso week from the start of the iso year using arithmetic.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement