Search code examples
postgresqldatetruncatedayofweekweekday

How to truncate a date to the beginning of week (Sunday)?


I need to truncate dates to the start of week, which is Sunday in my case. How can I do this in PostgreSQL? This truncates to Monday:

date_trunc('week', mydate)

Solution

  • If you subtract the dow value (0 for Sundays, 6 for Saturdays) from the current date than you get the previous Sunday which is the begin of your Sunday-based week

    demo:db<>fiddle

    SELECT 
        my_date - date_part('dow', my_date)::int
    FROM
        my_table
    

    Further reading, documentation