Help with date math - Mailing list pgsql-general

From Chris Hoover
Subject Help with date math
Date
Msg-id 1d219a6f0707200808m61d49693oe925c1626bf27fd5@mail.gmail.com
Whole thread Raw
Responses Re: Help with date math
Re: Help with date math
Re: Help with date math
List pgsql-general
I need some help.  I am trying to replicate a function from Sybase ASA, and am having difficulty.

I need to be able to subtract 2 date (or timestamps) and return the results expressed in days, weeks, month, quarters, or years.  How do I do this?

I believe Postgres is returning the number of days when you subtract to days.
i.e. postgres=# select current_date - '2007/01/01';
 ?column?
----------
      200
(1 row)



However, I can not figure out how to get the results expressed as 7 months, or 2 quarters, x weeks, or 0 years.  I have tried to use date_part, but since the subtraction is returning an integer, it is not happy.

Any help would be greatly appreciated.

Chris

P.S.

I'm trying to replicate Sybase's datediff function.

pgsql-general by date:

Previous
From: Vincenzo Romano
Date:
Subject: Difference between PRIMARY KEY index and UNIQUE-NOT NULL index
Next
From: Siah
Date:
Subject: Primary Key Performance with INTEGER vs. VARCHAR