pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Thomas Good <tomg@admin.nrnet.org>
To: PostgreSQL::SQL List <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] date_arithmetic revisited
Date: Sun, 1 Aug 1999 19:57:36 +0300
Message-ID: <l03130307b3ca2a5eb72f@[147.233.159.109]> (raw)
In-Reply-To: <Pine.LNX.3.96.990730101114.4153A-100000@admin.nrnet.org>

At 17:15 +0300 on 30/07/1999, Thomas Good wrote:


> Is there a way/function that allows me to do simple date arithmetic,
> ala, `SELECT $enddate - startdate;' ?

Sure. Nike.

I mean, just do it. What you get from it depends, of course, on the type of
the fields. If they are of type date, you get the round number of days
between them (the result is of type int4):

testing=> select d_start, d_end, d_end - d_start from test2;
   d_start|     d_end|?column?
----------+----------+--------
01-15-1969|08-01-1999|   11155
07-14-1999|07-18-1999|       4
04-13-1998|03-12-1998|     -32
12-01-1999|12-01-1999|       0
(4 rows)

If they are of type datetime, you get a timespan describing the difference:

testing=> select t_end - t_start as result from test1;
result
----------------------
@ 11154 days 23 hours
@ 4 days
@ 31 days 23 hours ago
@ 0
(4 rows)

If this doesn't suit you, you can take the date_part( 'day', ... ) from the
above operation, but it truncates rather than rounds. The result is float8,
BTW.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (3+ messages)  latest in thread

Message-ID: <l03130307b3ca2a5eb72f@[147.233.159.109]>
Permalink:  ../l03130307b3ca2a5eb72f@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03130307b3ca2a5eb72f@[147.233.159.109]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il, tomg@admin.nrnet.org
  Subject: Re: [SQL] date_arithmetic revisited
  In-Reply-To: <l03130307b3ca2a5eb72f@[147.233.159.109]>
  References: <Pine.LNX.3.96.990730101114.4153A-100000@admin.nrnet.org> <l03130307b3ca2a5eb72f@[147.233.159.109]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox