Thread: SQL output

SQL output

From
Simon Law
Date:
CREATE TABLE tablename (field INTERVAL);

INSERT INTO tablename VALUES('3 weeks');

SELECT field FROM tablename;
_________
|     field      |
|--------------|
|   21 days |
|________|

The output   shows up in days or months but not weeks how do i make  
it output in weeks?
Any help with this will be greatly appreciated

P.S.
Best postgresql book?



Re: SQL output

From
Mischa Sandberg
Date:
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.