Re: SQL output - Mailing list pgsql-sql

From Mischa Sandberg
Subject Re: SQL output
Date
Msg-id 1124151994.430132baa4e4e@webmail.telus.net
Whole thread Raw
In response to SQL output  (Simon Law <simonslaw@gmail.com>)
List pgsql-sql
Quoting Simon Law <simonslaw@gmail.com>:

> 
> CREATE TABLE tablename (field INTERVAL);
> INSERT INTO tablename VALUES('3 weeks');
> SELECT field FROM tablename;

> |   21 days |
> 
> The output   shows up in days or months but not weeks how do i make 
Internally, INTERVAL is stored as a 12byte tuple (years, months, days,
hours, minutes, seconds, microseconds). It discards any knowledge of
"weeks" (and "centuries" likewise) when it encodes the interval. So
there's no way to force it to say "weeks" back to you. There is no
datestyle that will do it for you, either.

You can MANUALLY extract the number of days in the interval, and divide
by 7 (round up or down, your choice).

SELECT EXTRACT(DAYS FROM INTERVAL '3 WEEKS')

Note, however, that if you define an interval with units greater than
days (i.e. months or years) you'll get nothing, which is reasonable:
months and years do not have fixed numbers of weeks in them.





pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Problem with a Pettern Matching Check
Next
From: Michael Fuhr
Date:
Subject: Re: Problem with a Pettern Matching Check