简体   繁体   中英

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

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’

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM