Mohammad Jahanzeb
Mohammad Jahanzeb

Reputation: 11

SQL Server SQL to PostgreSQL SQL

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

Upvotes: 0

Views: 75

Answers (1)

user330315
user330315

Reputation:

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

Upvotes: 1

Related Questions