4 CREATE TABLE DATE_TBL (f1 date);
5 INSERT INTO DATE_TBL VALUES ('1957-04-09');
6 INSERT INTO DATE_TBL VALUES ('1957-06-13');
7 INSERT INTO DATE_TBL VALUES ('1996-02-28');
8 INSERT INTO DATE_TBL VALUES ('1996-02-29');
9 INSERT INTO DATE_TBL VALUES ('1996-03-01');
10 INSERT INTO DATE_TBL VALUES ('1996-03-02');
11 INSERT INTO DATE_TBL VALUES ('1997-02-28');
12 INSERT INTO DATE_TBL VALUES ('1997-02-29');
13 ERROR: date/time field value out of range: "1997-02-29"
14 LINE 1: INSERT INTO DATE_TBL VALUES ('1997-02-29');
16 INSERT INTO DATE_TBL VALUES ('1997-03-01');
17 INSERT INTO DATE_TBL VALUES ('1997-03-02');
18 INSERT INTO DATE_TBL VALUES ('2000-04-01');
19 INSERT INTO DATE_TBL VALUES ('2000-04-02');
20 INSERT INTO DATE_TBL VALUES ('2000-04-03');
21 INSERT INTO DATE_TBL VALUES ('2038-04-08');
22 INSERT INTO DATE_TBL VALUES ('2039-04-09');
23 INSERT INTO DATE_TBL VALUES ('2040-04-10');
24 SELECT f1 AS "Fifteen" FROM DATE_TBL;
44 SELECT f1 AS "Nine" FROM DATE_TBL WHERE f1 < '2000-01-01';
58 SELECT f1 AS "Three" FROM DATE_TBL
59 WHERE f1 BETWEEN '2000-01-01' AND '2001-01-01';
68 -- Check all the documented input formats
70 SET datestyle TO iso; -- display results in ISO
72 SELECT date 'January 8, 1999';
78 SELECT date '1999-01-08';
84 SELECT date '1999-01-18';
90 SELECT date '1/8/1999';
91 ERROR: date/time field value out of range: "1/8/1999"
92 LINE 1: SELECT date '1/8/1999';
94 HINT: Perhaps you need a different "datestyle" setting.
95 SELECT date '1/18/1999';
96 ERROR: date/time field value out of range: "1/18/1999"
97 LINE 1: SELECT date '1/18/1999';
99 HINT: Perhaps you need a different "datestyle" setting.
100 SELECT date '18/1/1999';
101 ERROR: date/time field value out of range: "18/1/1999"
102 LINE 1: SELECT date '18/1/1999';
104 HINT: Perhaps you need a different "datestyle" setting.
105 SELECT date '01/02/03';
111 SELECT date '19990108';
117 SELECT date '990108';
123 SELECT date '1999.008';
129 SELECT date 'J2451187';
135 SELECT date 'January 8, 99 BC';
136 ERROR: date/time field value out of range: "January 8, 99 BC"
137 LINE 1: SELECT date 'January 8, 99 BC';
139 HINT: Perhaps you need a different "datestyle" setting.
140 SELECT date '99-Jan-08';
146 SELECT date '1999-Jan-08';
152 SELECT date '08-Jan-99';
153 ERROR: date/time field value out of range: "08-Jan-99"
154 LINE 1: SELECT date '08-Jan-99';
156 HINT: Perhaps you need a different "datestyle" setting.
157 SELECT date '08-Jan-1999';
163 SELECT date 'Jan-08-99';
164 ERROR: date/time field value out of range: "Jan-08-99"
165 LINE 1: SELECT date 'Jan-08-99';
167 HINT: Perhaps you need a different "datestyle" setting.
168 SELECT date 'Jan-08-1999';
174 SELECT date '99-08-Jan';
175 ERROR: invalid input syntax for type date: "99-08-Jan"
176 LINE 1: SELECT date '99-08-Jan';
178 SELECT date '1999-08-Jan';
179 ERROR: invalid input syntax for type date: "1999-08-Jan"
180 LINE 1: SELECT date '1999-08-Jan';
182 SELECT date '99 Jan 08';
188 SELECT date '1999 Jan 08';
194 SELECT date '08 Jan 99';
195 ERROR: date/time field value out of range: "08 Jan 99"
196 LINE 1: SELECT date '08 Jan 99';
198 HINT: Perhaps you need a different "datestyle" setting.
199 SELECT date '08 Jan 1999';
205 SELECT date 'Jan 08 99';
206 ERROR: date/time field value out of range: "Jan 08 99"
207 LINE 1: SELECT date 'Jan 08 99';
209 HINT: Perhaps you need a different "datestyle" setting.
210 SELECT date 'Jan 08 1999';
216 SELECT date '99 08 Jan';
222 SELECT date '1999 08 Jan';
228 SELECT date '99-01-08';
234 SELECT date '1999-01-08';
240 SELECT date '08-01-99';
241 ERROR: date/time field value out of range: "08-01-99"
242 LINE 1: SELECT date '08-01-99';
244 HINT: Perhaps you need a different "datestyle" setting.
245 SELECT date '08-01-1999';
246 ERROR: date/time field value out of range: "08-01-1999"
247 LINE 1: SELECT date '08-01-1999';
249 HINT: Perhaps you need a different "datestyle" setting.
250 SELECT date '01-08-99';
251 ERROR: date/time field value out of range: "01-08-99"
252 LINE 1: SELECT date '01-08-99';
254 HINT: Perhaps you need a different "datestyle" setting.
255 SELECT date '01-08-1999';
256 ERROR: date/time field value out of range: "01-08-1999"
257 LINE 1: SELECT date '01-08-1999';
259 HINT: Perhaps you need a different "datestyle" setting.
260 SELECT date '99-08-01';
266 SELECT date '1999-08-01';
272 SELECT date '99 01 08';
278 SELECT date '1999 01 08';
284 SELECT date '08 01 99';
285 ERROR: date/time field value out of range: "08 01 99"
286 LINE 1: SELECT date '08 01 99';
288 HINT: Perhaps you need a different "datestyle" setting.
289 SELECT date '08 01 1999';
290 ERROR: date/time field value out of range: "08 01 1999"
291 LINE 1: SELECT date '08 01 1999';
293 HINT: Perhaps you need a different "datestyle" setting.
294 SELECT date '01 08 99';
295 ERROR: date/time field value out of range: "01 08 99"
296 LINE 1: SELECT date '01 08 99';
298 HINT: Perhaps you need a different "datestyle" setting.
299 SELECT date '01 08 1999';
300 ERROR: date/time field value out of range: "01 08 1999"
301 LINE 1: SELECT date '01 08 1999';
303 HINT: Perhaps you need a different "datestyle" setting.
304 SELECT date '99 08 01';
310 SELECT date '1999 08 01';
316 SET datestyle TO dmy;
317 SELECT date 'January 8, 1999';
323 SELECT date '1999-01-08';
329 SELECT date '1999-01-18';
335 SELECT date '1/8/1999';
341 SELECT date '1/18/1999';
342 ERROR: date/time field value out of range: "1/18/1999"
343 LINE 1: SELECT date '1/18/1999';
345 HINT: Perhaps you need a different "datestyle" setting.
346 SELECT date '18/1/1999';
352 SELECT date '01/02/03';
358 SELECT date '19990108';
364 SELECT date '990108';
370 SELECT date '1999.008';
376 SELECT date 'J2451187';
382 SELECT date 'January 8, 99 BC';
388 SELECT date '99-Jan-08';
389 ERROR: date/time field value out of range: "99-Jan-08"
390 LINE 1: SELECT date '99-Jan-08';
392 HINT: Perhaps you need a different "datestyle" setting.
393 SELECT date '1999-Jan-08';
399 SELECT date '08-Jan-99';
405 SELECT date '08-Jan-1999';
411 SELECT date 'Jan-08-99';
417 SELECT date 'Jan-08-1999';
423 SELECT date '99-08-Jan';
424 ERROR: invalid input syntax for type date: "99-08-Jan"
425 LINE 1: SELECT date '99-08-Jan';
427 SELECT date '1999-08-Jan';
428 ERROR: invalid input syntax for type date: "1999-08-Jan"
429 LINE 1: SELECT date '1999-08-Jan';
431 SELECT date '99 Jan 08';
432 ERROR: date/time field value out of range: "99 Jan 08"
433 LINE 1: SELECT date '99 Jan 08';
435 HINT: Perhaps you need a different "datestyle" setting.
436 SELECT date '1999 Jan 08';
442 SELECT date '08 Jan 99';
448 SELECT date '08 Jan 1999';
454 SELECT date 'Jan 08 99';
460 SELECT date 'Jan 08 1999';
466 SELECT date '99 08 Jan';
467 ERROR: invalid input syntax for type date: "99 08 Jan"
468 LINE 1: SELECT date '99 08 Jan';
470 SELECT date '1999 08 Jan';
476 SELECT date '99-01-08';
477 ERROR: date/time field value out of range: "99-01-08"
478 LINE 1: SELECT date '99-01-08';
480 HINT: Perhaps you need a different "datestyle" setting.
481 SELECT date '1999-01-08';
487 SELECT date '08-01-99';
493 SELECT date '08-01-1999';
499 SELECT date '01-08-99';
505 SELECT date '01-08-1999';
511 SELECT date '99-08-01';
512 ERROR: date/time field value out of range: "99-08-01"
513 LINE 1: SELECT date '99-08-01';
515 HINT: Perhaps you need a different "datestyle" setting.
516 SELECT date '1999-08-01';
522 SELECT date '99 01 08';
523 ERROR: date/time field value out of range: "99 01 08"
524 LINE 1: SELECT date '99 01 08';
526 HINT: Perhaps you need a different "datestyle" setting.
527 SELECT date '1999 01 08';
533 SELECT date '08 01 99';
539 SELECT date '08 01 1999';
545 SELECT date '01 08 99';
551 SELECT date '01 08 1999';
557 SELECT date '99 08 01';
558 ERROR: date/time field value out of range: "99 08 01"
559 LINE 1: SELECT date '99 08 01';
561 HINT: Perhaps you need a different "datestyle" setting.
562 SELECT date '1999 08 01';
568 SET datestyle TO mdy;
569 SELECT date 'January 8, 1999';
575 SELECT date '1999-01-08';
581 SELECT date '1999-01-18';
587 SELECT date '1/8/1999';
593 SELECT date '1/18/1999';
599 SELECT date '18/1/1999';
600 ERROR: date/time field value out of range: "18/1/1999"
601 LINE 1: SELECT date '18/1/1999';
603 HINT: Perhaps you need a different "datestyle" setting.
604 SELECT date '01/02/03';
610 SELECT date '19990108';
616 SELECT date '990108';
622 SELECT date '1999.008';
628 SELECT date 'J2451187';
634 SELECT date 'January 8, 99 BC';
640 SELECT date '99-Jan-08';
641 ERROR: date/time field value out of range: "99-Jan-08"
642 LINE 1: SELECT date '99-Jan-08';
644 HINT: Perhaps you need a different "datestyle" setting.
645 SELECT date '1999-Jan-08';
651 SELECT date '08-Jan-99';
657 SELECT date '08-Jan-1999';
663 SELECT date 'Jan-08-99';
669 SELECT date 'Jan-08-1999';
675 SELECT date '99-08-Jan';
676 ERROR: invalid input syntax for type date: "99-08-Jan"
677 LINE 1: SELECT date '99-08-Jan';
679 SELECT date '1999-08-Jan';
680 ERROR: invalid input syntax for type date: "1999-08-Jan"
681 LINE 1: SELECT date '1999-08-Jan';
683 SELECT date '99 Jan 08';
684 ERROR: invalid input syntax for type date: "99 Jan 08"
685 LINE 1: SELECT date '99 Jan 08';
687 SELECT date '1999 Jan 08';
693 SELECT date '08 Jan 99';
699 SELECT date '08 Jan 1999';
705 SELECT date 'Jan 08 99';
711 SELECT date 'Jan 08 1999';
717 SELECT date '99 08 Jan';
718 ERROR: invalid input syntax for type date: "99 08 Jan"
719 LINE 1: SELECT date '99 08 Jan';
721 SELECT date '1999 08 Jan';
727 SELECT date '99-01-08';
728 ERROR: date/time field value out of range: "99-01-08"
729 LINE 1: SELECT date '99-01-08';
731 HINT: Perhaps you need a different "datestyle" setting.
732 SELECT date '1999-01-08';
738 SELECT date '08-01-99';
744 SELECT date '08-01-1999';
750 SELECT date '01-08-99';
756 SELECT date '01-08-1999';
762 SELECT date '99-08-01';
763 ERROR: date/time field value out of range: "99-08-01"
764 LINE 1: SELECT date '99-08-01';
766 HINT: Perhaps you need a different "datestyle" setting.
767 SELECT date '1999-08-01';
773 SELECT date '99 01 08';
774 ERROR: date/time field value out of range: "99 01 08"
775 LINE 1: SELECT date '99 01 08';
777 HINT: Perhaps you need a different "datestyle" setting.
778 SELECT date '1999 01 08';
784 SELECT date '08 01 99';
790 SELECT date '08 01 1999';
796 SELECT date '01 08 99';
802 SELECT date '01 08 1999';
808 SELECT date '99 08 01';
809 ERROR: date/time field value out of range: "99 08 01"
810 LINE 1: SELECT date '99 08 01';
812 HINT: Perhaps you need a different "datestyle" setting.
813 SELECT date '1999 08 01';
822 -- Leave most of it for the horology tests
824 SELECT f1 - date '2000-01-01' AS "Days From 2K" FROM DATE_TBL;
844 SELECT f1 - date 'epoch' AS "Days From Epoch" FROM DATE_TBL;
864 SELECT date 'yesterday' - date 'today' AS "One day";
870 SELECT date 'today' - date 'tomorrow' AS "One day";
876 SELECT date 'yesterday' - date 'tomorrow' AS "Two days";
882 SELECT date 'tomorrow' - date 'today' AS "One day";
888 SELECT date 'today' - date 'yesterday' AS "One day";
894 SELECT date 'tomorrow' - date 'yesterday' AS "Two days";
905 SELECT EXTRACT(CENTURY FROM DATE '0101-12-31 BC'); -- -2
911 SELECT EXTRACT(CENTURY FROM DATE '0100-12-31 BC'); -- -1
917 SELECT EXTRACT(CENTURY FROM DATE '0001-12-31 BC'); -- -1
923 SELECT EXTRACT(CENTURY FROM DATE '0001-01-01'); -- 1
929 SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD'); -- 1
935 SELECT EXTRACT(CENTURY FROM DATE '1900-12-31'); -- 19
941 SELECT EXTRACT(CENTURY FROM DATE '1901-01-01'); -- 20
947 SELECT EXTRACT(CENTURY FROM DATE '2000-12-31'); -- 20
953 SELECT EXTRACT(CENTURY FROM DATE '2001-01-01'); -- 21
959 SELECT EXTRACT(CENTURY FROM CURRENT_DATE)>=21 AS True; -- true
968 SELECT EXTRACT(MILLENNIUM FROM DATE '0001-12-31 BC'); -- -1
974 SELECT EXTRACT(MILLENNIUM FROM DATE '0001-01-01 AD'); -- 1
980 SELECT EXTRACT(MILLENNIUM FROM DATE '1000-12-31'); -- 1
986 SELECT EXTRACT(MILLENNIUM FROM DATE '1001-01-01'); -- 2
992 SELECT EXTRACT(MILLENNIUM FROM DATE '2000-12-31'); -- 2
998 SELECT EXTRACT(MILLENNIUM FROM DATE '2001-01-01'); -- 3
1004 -- next test to be fixed on the turn of the next millennium;-)
1005 SELECT EXTRACT(MILLENNIUM FROM CURRENT_DATE); -- 3
1014 SELECT EXTRACT(DECADE FROM DATE '1994-12-25'); -- 199
1020 SELECT EXTRACT(DECADE FROM DATE '0010-01-01'); -- 1
1026 SELECT EXTRACT(DECADE FROM DATE '0009-12-31'); -- 0
1032 SELECT EXTRACT(DECADE FROM DATE '0001-01-01 BC'); -- 0
1038 SELECT EXTRACT(DECADE FROM DATE '0002-12-31 BC'); -- -1
1044 SELECT EXTRACT(DECADE FROM DATE '0011-01-01 BC'); -- -1
1050 SELECT EXTRACT(DECADE FROM DATE '0012-12-31 BC'); -- -2
1057 -- some other types:
1060 SELECT EXTRACT(CENTURY FROM NOW())>=21 AS True; -- true
1066 SELECT EXTRACT(CENTURY FROM TIMESTAMP '1970-03-20 04:30:00.00000'); -- 20
1073 SELECT EXTRACT(CENTURY FROM INTERVAL '100 y'); -- 1
1079 SELECT EXTRACT(CENTURY FROM INTERVAL '99 y'); -- 0
1085 SELECT EXTRACT(CENTURY FROM INTERVAL '-99 y'); -- 0
1091 SELECT EXTRACT(CENTURY FROM INTERVAL '-100 y'); -- -1
1098 -- test trunc function!
1100 SELECT DATE_TRUNC('MILLENNIUM', TIMESTAMP '1970-03-20 04:30:00.00000'); -- 1001
1102 --------------------------
1103 Thu Jan 01 00:00:00 1001
1106 SELECT DATE_TRUNC('MILLENNIUM', DATE '1970-03-20'); -- 1001-01-01
1108 ------------------------------
1109 Thu Jan 01 00:00:00 1001 PST
1112 SELECT DATE_TRUNC('CENTURY', TIMESTAMP '1970-03-20 04:30:00.00000'); -- 1901
1114 --------------------------
1115 Tue Jan 01 00:00:00 1901
1118 SELECT DATE_TRUNC('CENTURY', DATE '1970-03-20'); -- 1901
1120 ------------------------------
1121 Tue Jan 01 00:00:00 1901 PST
1124 SELECT DATE_TRUNC('CENTURY', DATE '2004-08-10'); -- 2001-01-01
1126 ------------------------------
1127 Mon Jan 01 00:00:00 2001 PST
1130 SELECT DATE_TRUNC('CENTURY', DATE '0002-02-04'); -- 0001-01-01
1132 ------------------------------
1133 Mon Jan 01 00:00:00 0001 PST
1136 SELECT DATE_TRUNC('CENTURY', DATE '0055-08-10 BC'); -- 0100-01-01 BC
1138 ---------------------------------
1139 Tue Jan 01 00:00:00 0100 PST BC
1142 SELECT DATE_TRUNC('DECADE', DATE '1993-12-25'); -- 1990-01-01
1144 ------------------------------
1145 Mon Jan 01 00:00:00 1990 PST
1148 SELECT DATE_TRUNC('DECADE', DATE '0004-12-25'); -- 0001-01-01 BC
1150 ---------------------------------
1151 Sat Jan 01 00:00:00 0001 PST BC
1154 SELECT DATE_TRUNC('DECADE', DATE '0002-12-31 BC'); -- 0011-01-01 BC
1156 ---------------------------------
1157 Mon Jan 01 00:00:00 0011 PST BC
1163 select 'infinity'::date, '-infinity'::date;
1165 ----------+-----------
1166 infinity | -infinity
1169 select 'infinity'::date > 'today'::date as t;
1175 select '-infinity'::date < 'today'::date as t;
1181 select isfinite('infinity'::date), isfinite('-infinity'::date), isfinite('today'::date);
1182 isfinite | isfinite | isfinite
1183 ----------+----------+----------