Skip to content
Advertisement

DATEPART and DATEFIRST alternative for redshift

I’m trying to run

SELECT DATEPART(week, date(date)), MIN(date)

in Redshift with the start day being Saturday. I tried using different syntax for SET DATEFIRST but it seems like it’s not supported (or I just can’t find the correct syntax for redshift). Are there any alternatives available?

The documentation page only shows information for extracting a specific dow (day of week): https://docs.aws.amazon.com/redshift/latest/dg/r_DATE_PART_function.html

Advertisement

Answer

The date_trunc() function will truncate a timestamp to any number of levels including “week”. If memory serves a date_trunc to a week will set the result to midnight of the Monday before the given timestamp.

Now you want it to truncate to Saturday, 2 days earlier, and this is doable by adding 2 days before the date_trunc and then subtracting 2 days after. Like this (untested so forgive any typos):

date_trunc(‘week’, <your-timestamp> + interval ‘2 days’) - interval ‘2 days’
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement