0

I need to convert the following SQL server code to PostgreSQL code. Any help would be appreciated.

SQL Server SQL:

CAST(DATEADD(ww,DATEDIFF(ww,0,trans_date),-1)as date) as week
1
  • Including your PostgreSQL attempt will greatly help those you are asking for help from help you; we can't correct your attempt if we don't know what it was. Commented Aug 12, 2019 at 17:47

1 Answer 1

1

I think what that code does is to "round" the value of trans_date to the beginning of the week. In Postgres you can do that using the date_trunc() function:

date_trunc('week', trans_date)

Note that this always returns a timestamp, if you need a real date value, cast the result:

date_trunc('week', trans_date)::date

If it should be the day before the beginning of the week, just subtract one day from the result:

 date_trunc('week', trans_date)::date  - 1
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your answer! This worked perfectly! I actually needed that last one so just wanted to add "INTERVAL" which was missing from the code. date_trunc('week', trans_date)::date - INTERVAL '1 day'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.