I’m trying to migrate a Redshift SQL script to Snowflake and trying to emulate week number on Snowflake. Below is the sample Redshift code:
select cast(to_char('2020-01-06'::date, 'WW') as int) as week,
cast(to_char('2020-01-06'::date, 'IW') as int) as iso_week,
'2020-01-06'::date;
The closest functions I found on Snowflake was like this:
select cast(WEEKOFYEAR('2020-01-06'::date) as int) as week,
cast(WEEKISO('2020-01-06'::date) as int) as iso_week,
'2020-01-06'::date;
iso_week
fields are matching, however week
doesn’t [Redshift shows 1, Snowflake shows 2]. Is there any function that emulates Redshift’s behavior?
2
Answers
It is depenedent on parameter WEEK_OF_YEAR_POLICY
This is the definition of
WW
on RedshiftThen the equivalent is to get the "day of year", and divide by 7 to count the number of weeks. We will also need some +/- ones to account for base 0:
In UDF form: