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;
474 SELECT interval '1 2:03' day to minute;
480 SELECT interval '1 2:03:04' day to minute;
486 SELECT interval '1 2' day to second;
492 SELECT interval '1 2:03' day to second;
498 SELECT interval '1 2:03:04' day to second;
504 SELECT interval '1 2' hour to minute;
505 ERROR: invalid input syntax for type interval: "1 2"
506 LINE 1: SELECT interval '1 2' hour to minute;
508 SELECT interval '1 2:03' hour to minute;
514 SELECT interval '1 2:03:04' hour to minute;
520 SELECT interval '1 2' hour to second;
521 ERROR: invalid input syntax for type interval: "1 2"
522 LINE 1: SELECT interval '1 2' hour to second;
524 SELECT interval '1 2:03' hour to second;
530 SELECT interval '1 2:03:04' hour to second;
536 SELECT interval '1 2' minute to second;
537 ERROR: invalid input syntax for type interval: "1 2"
538 LINE 1: SELECT interval '1 2' minute to second;
540 SELECT interval '1 2:03' minute to second;
546 SELECT interval '1 2:03:04' minute to second;
552 SELECT interval '123 11' day to hour; -- ok
558 SELECT interval '123 11' day; -- not ok
559 ERROR: invalid input syntax for type interval: "123 11"
560 LINE 1: SELECT interval '123 11' day;
562 SELECT interval '123 11'; -- not ok, too ambiguous
563 ERROR: invalid input syntax for type interval: "123 11"
564 LINE 1: SELECT interval '123 11';
566 -- test syntaxes for restricted precision
567 SELECT interval(0) '1 day 01:23:45.6789';
573 SELECT interval(2) '1 day 01:23:45.6789';
579 SELECT interval '12:34.5678' minute to second(2); -- per SQL spec
585 SELECT interval(2) '12:34.5678' minute to second; -- historical PG
591 SELECT interval(2) '12:34.5678' minute to second(2); -- syntax error
592 ERROR: interval precision specified twice
593 LINE 1: SELECT interval(2) '12:34.5678' minute to second(2);
595 SELECT interval '1.234' second;
601 SELECT interval '1.234' second(2);
607 SELECT interval '1 2.345' day to second(2);
613 SELECT interval '1 2:03' day to second(2);
619 SELECT interval '1 2:03.4567' day to second(2);
625 SELECT interval '1 2:03:04.5678' day to second(2);
631 SELECT interval '1 2.345' hour to second(2);
632 ERROR: invalid input syntax for type interval: "1 2.345"
633 LINE 1: SELECT interval '1 2.345' hour to second(2);
635 SELECT interval '1 2:03.45678' hour to second(2);
641 SELECT interval '1 2:03:04.5678' hour to second(2);
647 SELECT interval '1 2.3456' minute to second(2);
648 ERROR: invalid input syntax for type interval: "1 2.3456"
649 LINE 1: SELECT interval '1 2.3456' minute to second(2);
651 SELECT interval '1 2:03.5678' minute to second(2);
657 SELECT interval '1 2:03:04.5678' minute to second(2);
663 -- test inputting and outputting SQL standard interval literals
664 SET IntervalStyle TO sql_standard;
665 SELECT interval '0' AS "zero",
666 interval '1-2' year to month AS "year-month",
667 interval '1 2:03:04' day to second AS "day-time",
668 - interval '1-2' AS "negative year-month",
669 - interval '1 2:03:04' AS "negative day-time";
670 zero | year-month | day-time | negative year-month | negative day-time
671 ------+------------+-----------+---------------------+-------------------
672 0 | 1-2 | 1 2:03:04 | -1-2 | -1 2:03:04
675 -- test input of some not-quite-standard interval values in the sql style
676 SET IntervalStyle TO postgres;
677 SELECT interval '+1 -1:00:00',
678 interval '-1 +1:00:00',
679 interval '+1-2 -3 +4:05:06.789',
680 interval '-1-2 +3 -4:05:06.789';
681 interval | interval | interval | interval
682 -----------------+-------------------+-------------------------------------+----------------------------------------
683 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
686 -- test output of couple non-standard interval values in the sql style
687 SET IntervalStyle TO sql_standard;
688 SELECT interval '1 day -1 hours',
689 interval '-1 days +1 hours',
690 interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
691 - interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';
692 interval | interval | interval | ?column?
693 ------------------+------------------+----------------------+----------------------
694 +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
697 -- test outputting iso8601 intervals
698 SET IntervalStyle to iso_8601;
699 select interval '0' AS "zero",
700 interval '1-2' AS "a year 2 months",
701 interval '1 2:03:04' AS "a bit over a day",
702 interval '2:03:04.45679' AS "a bit over 2 hours",
703 (interval '1-2' + interval '3 4:05:06.7') AS "all fields",
704 (interval '1-2' - interval '3 4:05:06.7') AS "mixed sign",
705 (- interval '1-2' + interval '3 4:05:06.7') AS "negative";
706 zero | a year 2 months | a bit over a day | a bit over 2 hours | all fields | mixed sign | negative
707 ------+-----------------+------------------+--------------------+------------------+----------------------+--------------------
708 PT0S | P1Y2M | P1DT2H3M4S | PT2H3M4.45679S | P1Y2M3DT4H5M6.7S | P1Y2M-3DT-4H-5M-6.7S | P-1Y-2M3DT4H5M6.7S
711 -- test inputting ISO 8601 4.4.2.1 "Format With Time Unit Designators"
712 SET IntervalStyle to sql_standard;
713 select interval 'P0Y' AS "zero",
714 interval 'P1Y2M' AS "a year 2 months",
715 interval 'P1W' AS "a week",
716 interval 'P1DT2H3M4S' AS "a bit over a day",
717 interval 'P1Y2M3DT4H5M6.7S' AS "all fields",
718 interval 'P-1Y-2M-3DT-4H-5M-6.7S' AS "negative",
719 interval 'PT-0.1S' AS "fractional second";
720 zero | a year 2 months | a week | a bit over a day | all fields | negative | fractional second
721 ------+-----------------+-----------+------------------+--------------------+--------------------+-------------------
722 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
725 -- test inputting ISO 8601 4.4.2.2 "Alternative Format"
726 SET IntervalStyle to postgres;
727 select interval 'P00021015T103020' AS "ISO8601 Basic Format",
728 interval 'P0002-10-15T10:30:20' AS "ISO8601 Extended Format";
729 ISO8601 Basic Format | ISO8601 Extended Format
730 ----------------------------------+----------------------------------
731 2 years 10 mons 15 days 10:30:20 | 2 years 10 mons 15 days 10:30:20
734 -- Make sure optional ISO8601 alternative format fields are optional.
735 select interval 'P0002' AS "year only",
736 interval 'P0002-10' AS "year month",
737 interval 'P0002-10-15' AS "year month day",
738 interval 'P0002T1S' AS "year only plus time",
739 interval 'P0002-10T1S' AS "year month plus time",
740 interval 'P0002-10-15T1S' AS "year month day plus time",
741 interval 'PT10' AS "hour only",
742 interval 'PT10:30' AS "hour minute";
743 year only | year month | year month day | year only plus time | year month plus time | year month day plus time | hour only | hour minute
744 -----------+-----------------+-------------------------+---------------------+--------------------------+----------------------------------+-----------+-------------
745 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
748 -- test a couple rounding cases that changed since 8.3 w/ HAVE_INT64_TIMESTAMP.
749 SET IntervalStyle to postgres_verbose;
750 select interval '-10 mons -3 days +03:55:06.70';
752 --------------------------------------------------
753 @ 10 mons 3 days -3 hours -55 mins -6.7 secs ago
756 select interval '1 year 2 mons 3 days 04:05:06.699999';
758 -----------------------------------------------------
759 @ 1 year 2 mons 3 days 4 hours 5 mins 6.699999 secs
762 select interval '0:0:0.7', interval '@ 0.70 secs', interval '0.7 seconds';
763 interval | interval | interval
764 ------------+------------+------------
765 @ 0.7 secs | @ 0.7 secs | @ 0.7 secs
768 -- check that '30 days' equals '1 month' according to the hash function
769 select '30 days'::interval = '1 month'::interval as t;
775 select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;