5 SET IntervalStyle to postgres;
6 -- check acceptance of "time zone style"
7 SELECT INTERVAL '01:00' AS "One hour";
13 SELECT INTERVAL '+02:00' AS "Two hours";
19 SELECT INTERVAL '-08:00' AS "Eight hours";
25 SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
31 SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
37 SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
39 -----------------------
43 SELECT INTERVAL '1.5 months' AS "One month 15 days";
49 SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
51 ----------------------------------
52 9 years 1 mon -12 days +13:14:00
55 CREATE TABLE INTERVAL_TBL (f1 interval);
56 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
57 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
58 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
59 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
60 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
61 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
62 INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
63 INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
64 INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
65 INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
66 -- badly formatted interval
67 INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
68 ERROR: invalid input syntax for type interval: "badly formatted interval"
69 LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted inter...
71 INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
72 ERROR: invalid input syntax for type interval: "@ 30 eons ago"
73 LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
75 -- test interval operators
76 SELECT '' AS ten, * FROM INTERVAL_TBL;
78 -----+-----------------
91 SELECT '' AS nine, * FROM INTERVAL_TBL
92 WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
94 ------+-----------------
106 SELECT '' AS three, * FROM INTERVAL_TBL
107 WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
115 SELECT '' AS three, * FROM INTERVAL_TBL
116 WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
124 SELECT '' AS one, * FROM INTERVAL_TBL
125 WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
131 SELECT '' AS five, * FROM INTERVAL_TBL
132 WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
134 ------+-----------------
142 SELECT '' AS nine, * FROM INTERVAL_TBL
143 WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
145 ------+-----------------
157 SELECT '' AS fortyfive, r1.*, r2.*
158 FROM INTERVAL_TBL r1, INTERVAL_TBL r2
160 ORDER BY r1.f1, r2.f1;
162 -----------+-----------------+-----------------
163 | 00:01:00 | -00:00:14
164 | 05:00:00 | -00:00:14
165 | 05:00:00 | 00:01:00
166 | 1 day 02:03:04 | -00:00:14
167 | 1 day 02:03:04 | 00:01:00
168 | 1 day 02:03:04 | 05:00:00
169 | 10 days | -00:00:14
172 | 10 days | 1 day 02:03:04
176 | 3 mons | 1 day 02:03:04
181 | 5 mons | 1 day 02:03:04
184 | 5 mons 12:00:00 | -00:00:14
185 | 5 mons 12:00:00 | 00:01:00
186 | 5 mons 12:00:00 | 05:00:00
187 | 5 mons 12:00:00 | 1 day 02:03:04
188 | 5 mons 12:00:00 | 10 days
189 | 5 mons 12:00:00 | 3 mons
190 | 5 mons 12:00:00 | 5 mons
191 | 6 years | -00:00:14
194 | 6 years | 1 day 02:03:04
198 | 6 years | 5 mons 12:00:00
199 | 34 years | -00:00:14
200 | 34 years | 00:01:00
201 | 34 years | 05:00:00
202 | 34 years | 1 day 02:03:04
206 | 34 years | 5 mons 12:00:00
210 -- Test multiplication and division with intervals.
211 -- Floating point arithmetic rounding errors can lead to unexpected results,
212 -- though the code attempts to do the right thing and round up to days and
213 -- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'.
214 -- Note that it is expected for some day components to be greater than 29 and
215 -- some time components be greater than 23:59:59 due to how intervals are
216 -- stored internally.
217 CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
218 COPY INTERVAL_MULDIV_TBL FROM STDIN;
219 SELECT span * 0.3 AS product
220 FROM INTERVAL_MULDIV_TBL;
222 ------------------------------------
223 1 year 12 days 122:24:00
224 -1 years -12 days +93:36:00
226 2 mons 13 days 01:22:28.8
227 -10 mons +120 days 37:28:21.6567
230 24 years 11 mons 320 days 16:48:00
233 SELECT span * 8.2 AS product
234 FROM INTERVAL_MULDIV_TBL;
236 ---------------------------------------------
237 28 years 104 days 2961:36:00
238 -28 years -104 days +2942:24:00
240 6 years 1 mon -197 days +93:34:27.2
241 -24 years -7 mons +3946 days 640:15:11.9498
242 2 years 8 mons 24 days
243 9 years 6 mons 24 days
244 682 years 7 mons 8215 days 19:12:00
247 SELECT span / 10 AS quotient
248 FROM INTERVAL_MULDIV_TBL;
250 ----------------------------------
251 4 mons 4 days 40:48:00
252 -4 mons -4 days +31:12:00
255 -3 mons +30 days 12:29:27.2189
258 8 years 3 mons 126 days 21:36:00
261 SELECT span / 100 AS quotient
262 FROM INTERVAL_MULDIV_TBL;
264 -------------------------
269 -6 days +01:14:56.72189
272 9 mons 39 days 16:33:36
275 DROP TABLE INTERVAL_MULDIV_TBL;
276 SET DATESTYLE = 'postgres';
277 SET IntervalStyle to postgres_verbose;
278 SELECT '' AS ten, * FROM INTERVAL_TBL;
280 -----+-------------------------------
287 | @ 1 day 2 hours 3 mins 4 secs
293 -- test avg(interval), which is somewhat fragile since people have been
294 -- known to change the allowed input syntax for type interval without
295 -- updating pg_aggregate.agginitval
296 select avg(f1) from interval_tbl;
298 -------------------------------------------------
299 @ 4 years 1 mon 10 days 4 hours 18 mins 23 secs
302 -- test long interval input
303 select '4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'::interval;
305 --------------------------------------------
306 @ 4541 years 4 mons 4 days 17 mins 31 secs
309 -- test justify_hours() and justify_days()
310 SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as "6 mons 5 days 4 hours 3 mins 2 seconds";
311 6 mons 5 days 4 hours 3 mins 2 seconds
312 ----------------------------------------
313 @ 6 mons 5 days 4 hours 3 mins 2 secs
316 SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as "7 mons 6 days 5 hours 4 mins 3 seconds";
317 7 mons 6 days 5 hours 4 mins 3 seconds
318 ----------------------------------------
319 @ 7 mons 6 days 5 hours 4 mins 3 secs
322 -- test justify_interval()
323 SELECT justify_interval(interval '1 month -1 hour') as "1 month -1 hour";
329 -- test fractional second input, and detection of duplicate units
330 SET DATESTYLE = 'ISO';
331 SET IntervalStyle TO postgres;
332 SELECT '1 millisecond'::interval, '1 microsecond'::interval,
333 '500 seconds 99 milliseconds 51 microseconds'::interval;
334 interval | interval | interval
335 --------------+-----------------+-----------------
336 00:00:00.001 | 00:00:00.000001 | 00:08:20.099051
339 SELECT '3 days 5 milliseconds'::interval;
341 ---------------------
345 SELECT '1 second 2 seconds'::interval; -- error
346 ERROR: invalid input syntax for type interval: "1 second 2 seconds"
347 LINE 1: SELECT '1 second 2 seconds'::interval;
349 SELECT '10 milliseconds 20 milliseconds'::interval; -- error
350 ERROR: invalid input syntax for type interval: "10 milliseconds 20 milliseconds"
351 LINE 1: SELECT '10 milliseconds 20 milliseconds'::interval;
353 SELECT '5.5 seconds 3 milliseconds'::interval; -- error
354 ERROR: invalid input syntax for type interval: "5.5 seconds 3 milliseconds"
355 LINE 1: SELECT '5.5 seconds 3 milliseconds'::interval;
357 SELECT '1:20:05 5 microseconds'::interval; -- error
358 ERROR: invalid input syntax for type interval: "1:20:05 5 microseconds"
359 LINE 1: SELECT '1:20:05 5 microseconds'::interval;
361 SELECT '1 day 1 day'::interval; -- error
362 ERROR: invalid input syntax for type interval: "1 day 1 day"
363 LINE 1: SELECT '1 day 1 day'::interval;
365 SELECT interval '1-2'; -- SQL year-month literal
371 SELECT interval '999' second; -- oversize leading field is ok
377 SELECT interval '999' minute;
383 SELECT interval '999' hour;
389 SELECT interval '999' day;
395 SELECT interval '999' month;
401 -- test SQL-spec syntaxes for restricted field sets
402 SELECT interval '1' year;
408 SELECT interval '2' month;
414 SELECT interval '3' day;
420 SELECT interval '4' hour;
426 SELECT interval '5' minute;
432 SELECT interval '6' second;
438 SELECT interval '1' year to month;
444 SELECT interval '1-2' year to month;
450 SELECT interval '1 2' day to hour;
456 SELECT interval '1 2:03' day to hour;
462 SELECT interval '1 2:03:04' day to hour;
468 SELECT interval '1 2' day to minute;
469 ERROR: invalid input syntax for type interval: "1 2"
470 LINE 1: SELECT interval '1 2' day to minute;
472 SELECT interval '1 2:03' day to minute;
478 SELECT interval '1 2:03:04' day to minute;
484 SELECT interval '1 2' day to second;
485 ERROR: invalid input syntax for type interval: "1 2"
486 LINE 1: SELECT interval '1 2' day to second;
488 SELECT interval '1 2:03' day to second;
494 SELECT interval '1 2:03:04' day to second;
500 SELECT interval '1 2' hour to minute;
501 ERROR: invalid input syntax for type interval: "1 2"
502 LINE 1: SELECT interval '1 2' hour to minute;
504 SELECT interval '1 2:03' hour to minute;
510 SELECT interval '1 2:03:04' hour to minute;
516 SELECT interval '1 2' hour to second;
517 ERROR: invalid input syntax for type interval: "1 2"
518 LINE 1: SELECT interval '1 2' hour to second;
520 SELECT interval '1 2:03' hour to second;
526 SELECT interval '1 2:03:04' hour to second;
532 SELECT interval '1 2' minute to second;
533 ERROR: invalid input syntax for type interval: "1 2"
534 LINE 1: SELECT interval '1 2' minute to second;
536 SELECT interval '1 2:03' minute to second;
542 SELECT interval '1 2:03:04' minute to second;
548 SELECT interval '123 11' day to hour; -- ok
554 SELECT interval '123 11' day; -- not ok
555 ERROR: invalid input syntax for type interval: "123 11"
556 LINE 1: SELECT interval '123 11' day;
558 SELECT interval '123 11'; -- not ok, too ambiguous
559 ERROR: invalid input syntax for type interval: "123 11"
560 LINE 1: SELECT interval '123 11';
562 -- test syntaxes for restricted precision
563 SELECT interval(0) '1 day 01:23:45.6789';
569 SELECT interval(2) '1 day 01:23:45.6789';
575 SELECT interval '12:34.5678' minute to second(2); -- per SQL spec
581 SELECT interval(2) '12:34.5678' minute to second; -- historical PG
587 SELECT interval(2) '12:34.5678' minute to second(2); -- syntax error
588 ERROR: interval precision specified twice
589 LINE 1: SELECT interval(2) '12:34.5678' minute to second(2);
591 SELECT interval '1.234' second;
597 SELECT interval '1.234' second(2);
603 SELECT interval '1 2.345' day to second(2);
604 ERROR: invalid input syntax for type interval: "1 2.345"
605 LINE 1: SELECT interval '1 2.345' day to second(2);
607 SELECT interval '1 2:03' day to second(2);
613 SELECT interval '1 2:03.4567' day to second(2);
619 SELECT interval '1 2:03:04.5678' day to second(2);
625 SELECT interval '1 2.345' hour to second(2);
626 ERROR: invalid input syntax for type interval: "1 2.345"
627 LINE 1: SELECT interval '1 2.345' hour to second(2);
629 SELECT interval '1 2:03.45678' hour to second(2);
635 SELECT interval '1 2:03:04.5678' hour to second(2);
641 SELECT interval '1 2.3456' minute to second(2);
642 ERROR: invalid input syntax for type interval: "1 2.3456"
643 LINE 1: SELECT interval '1 2.3456' minute to second(2);
645 SELECT interval '1 2:03.5678' minute to second(2);
651 SELECT interval '1 2:03:04.5678' minute to second(2);
657 -- test inputting and outputting SQL standard interval literals
658 SET IntervalStyle TO sql_standard;
659 SELECT interval '0' AS "zero",
660 interval '1-2' year to month AS "year-month",
661 interval '1 2:03:04' day to second AS "day-time",
662 - interval '1-2' AS "negative year-month",
663 - interval '1 2:03:04' AS "negative day-time";
664 zero | year-month | day-time | negative year-month | negative day-time
665 ------+------------+-----------+---------------------+-------------------
666 0 | 1-2 | 1 2:03:04 | -1-2 | -1 2:03:04
669 -- test input of some not-quite-standard interval values in the sql style
670 SET IntervalStyle TO postgres;
671 SELECT interval '+1 -1:00:00',
672 interval '-1 +1:00:00',
673 interval '+1-2 -3 +4:05:06.789',
674 interval '-1-2 +3 -4:05:06.789';
675 interval | interval | interval | interval
676 -----------------+-------------------+-------------------------------------+----------------------------------------
677 1 day -01:00:00 | -1 days +01:00:00 | 1 year 2 mons -3 days +04:05:06.789 | -1 years -2 mons +3 days -04:05:06.789
680 -- test output of couple non-standard interval values in the sql style
681 SET IntervalStyle TO sql_standard;
682 SELECT interval '1 day -1 hours',
683 interval '-1 days +1 hours',
684 interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
685 - interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';
686 interval | interval | interval | ?column?
687 ------------------+------------------+----------------------+----------------------
688 +0-0 +1 -1:00:00 | +0-0 -1 +1:00:00 | +1-2 -3 +4:05:06.789 | -1-2 +3 -4:05:06.789
691 -- test outputting iso8601 intervals
692 SET IntervalStyle to iso_8601;
693 select interval '0' AS "zero",
694 interval '1-2' AS "a year 2 months",
695 interval '1 2:03:04' AS "a bit over a day",
696 interval '2:03:04.45679' AS "a bit over 2 hours",
697 (interval '1-2' + interval '3 4:05:06.7') AS "all fields",
698 (interval '1-2' - interval '3 4:05:06.7') AS "mixed sign",
699 (- interval '1-2' + interval '3 4:05:06.7') AS "negative";
700 zero | a year 2 months | a bit over a day | a bit over 2 hours | all fields | mixed sign | negative
701 ------+-----------------+------------------+--------------------+------------------+----------------------+--------------------
702 PT0S | P1Y2M | P1DT2H3M4S | PT2H3M4.45679S | P1Y2M3DT4H5M6.7S | P1Y2M-3DT-4H-5M-6.7S | P-1Y-2M3DT4H5M6.7S
705 -- test inputting ISO 8601 4.4.2.1 "Format With Time Unit Designators"
706 SET IntervalStyle to sql_standard;
707 select interval 'P0Y' AS "zero",
708 interval 'P1Y2M' AS "a year 2 months",
709 interval 'P1W' AS "a week",
710 interval 'P1DT2H3M4S' AS "a bit over a day",
711 interval 'P1Y2M3DT4H5M6.7S' AS "all fields",
712 interval 'P-1Y-2M-3DT-4H-5M-6.7S' AS "negative",
713 interval 'PT-0.1S' AS "fractional second";
714 zero | a year 2 months | a week | a bit over a day | all fields | negative | fractional second
715 ------+-----------------+-----------+------------------+--------------------+--------------------+-------------------
716 0 | 1-2 | 7 0:00:00 | 1 2:03:04 | +1-2 +3 +4:05:06.7 | -1-2 -3 -4:05:06.7 | -0:00:00.1
719 -- test inputting ISO 8601 4.4.2.2 "Alternative Format"
720 SET IntervalStyle to postgres;
721 select interval 'P00021015T103020' AS "ISO8601 Basic Format",
722 interval 'P0002-10-15T10:30:20' AS "ISO8601 Extended Format";
723 ISO8601 Basic Format | ISO8601 Extended Format
724 ----------------------------------+----------------------------------
725 2 years 10 mons 15 days 10:30:20 | 2 years 10 mons 15 days 10:30:20
728 -- Make sure optional ISO8601 alternative format fields are optional.
729 select interval 'P0002' AS "year only",
730 interval 'P0002-10' AS "year month",
731 interval 'P0002-10-15' AS "year month day",
732 interval 'P0002T1S' AS "year only plus time",
733 interval 'P0002-10T1S' AS "year month plus time",
734 interval 'P0002-10-15T1S' AS "year month day plus time",
735 interval 'PT10' AS "hour only",
736 interval 'PT10:30' AS "hour minute";
737 year only | year month | year month day | year only plus time | year month plus time | year month day plus time | hour only | hour minute
738 -----------+-----------------+-------------------------+---------------------+--------------------------+----------------------------------+-----------+-------------
739 2 years | 2 years 10 mons | 2 years 10 mons 15 days | 2 years 00:00:01 | 2 years 10 mons 00:00:01 | 2 years 10 mons 15 days 00:00:01 | 10:00:00 | 10:30:00
742 -- test a couple rounding cases that changed since 8.3 w/ HAVE_INT64_TIMESTAMP.
743 SET IntervalStyle to postgres_verbose;
744 select interval '-10 mons -3 days +03:55:06.70';
746 --------------------------------------------------
747 @ 10 mons 3 days -3 hours -55 mins -6.7 secs ago
750 select interval '1 year 2 mons 3 days 04:05:06.699999';
752 -----------------------------------------------------
753 @ 1 year 2 mons 3 days 4 hours 5 mins 6.699999 secs
756 select interval '0:0:0.7', interval '@ 0.70 secs', interval '0.7 seconds';
757 interval | interval | interval
758 ------------+------------+------------
759 @ 0.7 secs | @ 0.7 secs | @ 0.7 secs
762 -- check that '30 days' equals '1 month' according to the hash function
763 select '30 days'::interval = '1 month'::interval as t;
769 select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;