| The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference
by The PostgreSQL Global Development Group Paperback (6"x9"), 454 pages ISBN 9781906966041 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
6.5.4 Interval Input
interval values can be written using the following
verbose syntax:
[@] quantity unit [quantity unit...] [direction]
where quantity is a number (possibly signed);
unit is microsecond,
millisecond, second,
minute, hour, day,
week, month, year,
decade, century, millennium,
or abbreviations or plurals of these units;
direction can be ago or
empty. The at sign (@) is optional noise. The amounts
of the different units are implicitly added with appropriate
sign accounting. ago negates all the fields.
This syntax is also used for interval output, if
IntervalStyle is set to
postgres_verbose.
Quantities of days, hours, minutes, and seconds can be specified without
explicit unit markings. For example, '1 12:59:10' is read
the same as '1 day 12 hours 59 min 10 sec'. Also,
a combination of years and months can be specified with a dash;
for example '200-10' is read the same as '200 years
10 months'. (These shorter forms are in fact the only ones allowed
by the SQL standard, and are used for output when
IntervalStyle is set to sql_standard.)
Interval values can also be written as ISO 8601 time intervals, using either the “format with designators” of the standard's section 4.4.3.2 or the “alternative format” of section 4.4.3.3. The format with designators looks like this:
P quantity unit [ quantity unit ...] [ T [ quantity unit ...]]
The string must start with a P, and may include a
T that introduces the time-of-day units. The
available unit abbreviations are given in Table 6-16. Units may be
omitted, and may be specified in any order, but units smaller than
a day must appear after T. In particular, the meaning of
M depends on whether it is before or after
T.
| Abbreviation | Meaning
|
| Y | Years
|
| M | Months (in the date part)
|
| W | Weeks
|
| D | Days
|
| H | Hours
|
| M | Minutes (in the time part)
|
| S | Seconds |
In the alternative format:
P [ years-months-days ] [ T hours:minutes:seconds ]
the string must begin with P, and a
T separates the date and time parts of the interval.
The values are given as numbers similar to ISO 8601 dates.
When writing an interval constant with a fields
specification, or when assigning a string to an interval column that was
defined with a fields specification, the interpretation of
unmarked quantities depends on the fields. For
example INTERVAL '1' YEAR is read as 1 year, whereas
INTERVAL '1' means 1 second. Also, field values
“to the right” of the least significant field allowed by the
fields specification are silently discarded. For
example, writing INTERVAL '1 day 2:03:04' HOUR TO MINUTE
results in dropping the seconds field, but not the day field.
According to the SQL standard all fields of an interval
value must have the same sign, so a leading negative sign applies to all
fields; for example the negative sign in the interval literal
'-1 2:03:04' applies to both the days and hour/minute/second
parts. PostgreSQL allows the fields to have different
signs, and traditionally treats each field in the textual representation
as independently signed, so that the hour/minute/second part is
considered positive in this example. If IntervalStyle is
set to sql_standard then a leading sign is considered
to apply to all fields (but only if no additional signs appear).
Otherwise the traditional PostgreSQL interpretation is
used. To avoid ambiguity, it's recommended to attach an explicit sign
to each field if any field is negative.
Internally interval values are stored as months, days,
and seconds. This is done because the number of days in a month
varies, and a day can have 23 or 25 hours if a daylight savings
time adjustment is involved. The months and days fields are integers
while the seconds field can store fractions. Because intervals are
usually created from constant strings or timestamp subtraction,
this storage method works well in most cases. Functions
justify_days and justify_hours are
available for adjusting days and hours that overflow their normal
ranges.
In the verbose input format, and in some fields of the more compact
input formats, field values can have fractional parts; for example
'1.5 week' or '01:02:03.45'. Such input is
converted to the appropriate number of months, days, and seconds
for storage. When this would result in a fractional number of
months or days, the fraction is added to the lower-order fields
using the conversion factors 1 month = 30 days and 1 day = 24 hours.
For example, '1.5 month' becomes 1 month and 15 days.
Only seconds will ever be shown as fractional on output.
Table 6-17 shows some examples
of valid interval input.
| Example | Description
|
| 1-2 | SQL standard format: 1 year 2 months
|
| 3 4:05:06 | SQL standard format: 3 days 4 hours 5 minutes 6 seconds
|
| 1 year 2 months 3 days 4 hours 5 minutes 6 seconds | Traditional Postgres format: 1 year 2 months 3 days 4 hours 5 minutes 6 seconds
|
| P1Y2M3DT4H5M6S | ISO 8601 “format with designators”: same meaning as above
|
| P0001-02-03T04:05:06 | ISO 8601 “alternative format”: same meaning as above |
| ISBN 9781906966041 | The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference | See the print edition |