DateDiff() function - Mailing list pgsql-sql

From Huan Ruan
Subject DateDiff() function
Date
Msg-id CAD1stZuwG5fx4YJ+dyFRuxEaCmYo+nUWJvwuP-=FUTEA463PdQ@mail.gmail.com
Whole thread Raw
Responses Re: DateDiff() function
List pgsql-sql
Hi Guys

We are migrating to Postgres. In the current system, we use datediff() function to get the difference between two dates, e.g. datediff (month, cast('2013-01-01' as timestamp), cast('2013-02-02' as timestamp) returns 1.

I understand that Postgres has Interval data type so I can achieve the same with Extract(month from Age(date1, date2)). However, I try to make it so that the existing SQL can run on both databases without changes. One possible way is to add a datediff function to Postgres, but the problem is that month/day/year etc is a keyword not a string like 'month'. I noticed that Postgres seems to convert Extract(month from current_timestamp) to date_part('month', current_timestamp), you can also do Extract('month' from current_timestamp). So it seems internally, Postgres can do the mapping from month to 'month'. I was wondering if there is a way for me to do the same for the datediff() function? Any other ideas?

Thanks
Huan

pgsql-sql by date:

Previous
From: Igor Neyman
Date:
Subject: Re: delete where not in another table
Next
From: Gavin Flower
Date:
Subject: Re: DateDiff() function