Skip to content

Commit b09ffc5

Browse files
authored
Create postgresql12.4-interval code snippet
1 parent a75477f commit b09ffc5

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

postgresql12.4-interval code snippet

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
'''
4+
5+
this snippet shows the date time postgresql arithmetic for postgresql 12.4
6+
'''
7+
8+
CREATE TABLE if not exists trips (
9+
id serial PRIMARY KEY,
10+
completed_date TIMESTAMP NOT NULL,
11+
brand varchar(10)
12+
13+
);
14+
15+
16+
insert into trips(id, completed_date, brand)
17+
values
18+
(1, '2022-01-12', 'kia'),
19+
(2, '2022-02-13', 'honda'),
20+
(3, '2022-02-14', 'k'),
21+
(4, '2022-02-15', 'lzz'),
22+
(5, '2022-02-16', 'lzz'),
23+
(6, '2022-02-17', 'lzz'),
24+
(7, '2022-02-18', 'lzz'),
25+
(8, '2022-02-19', 'lzz'),
26+
(9, '2022-02-20', 'lzz'),
27+
(10, '2022-02-21', 'lzz'),
28+
(11, '2022-02-22', 'lzz'),
29+
(12, '2022-02-23', 'lzz'),
30+
(13, '2022-02-24', 'lzz'),
31+
(14, '2022-02-25', 'lzz'),
32+
(15, '2022-02-25', 'lzz'),
33+
(16, '2022-02-26', 'lzz'),
34+
(17, '2022-02-27', 'lzz'),
35+
(18, '2022-02-28', 'lzz'),
36+
(22, '2022-03-01', 'lzz'),
37+
(23, '2022-03-02', 'lzz'),
38+
(24, '2022-03-03', 'lzz'),
39+
(25, '2022-03-04', 'lzz'),
40+
(26, '2022-03-05', 'lzz'),
41+
(27, '2022-03-06', 'lzz'),
42+
(28, '2022-03-07', 'lzz'),
43+
(29, '2022-03-08', 'lzz'),
44+
(30, '2022-03-09', 'lzz');
45+
46+
специально выбрал посередине дату - 28 фев 2022
47+
select * from trips where completed_date > '2022-02-28'::date - INTERVAL '3 DAY'; #will show dates after 28 feb-3 days - after 26 feb:
48+
49+
id 1 completed_date 1 brand
50+
16 2022-02-26 00:00:00 I lzz
51+
17 2022-02-27 00:00:00 I lzz
52+
18 2022-02-28 00:00:00 I lzz
53+
22 2022-03-01 00:00:00 I lzz
54+
23 2022-03-02 00:00:00 I lzz
55+
24 2022-03-03 00:00:00 I lzz
56+
25 2022-03-04 00:00:00 I lzz
57+
26 2022-03-05 00:00:00 I lzz
58+
27 2022-03-06 00:00:00 I lzz
59+
28 2022-03-07 00:00:00 I lzz
60+
29 2022-03-08 00:00:00 I lzz
61+
30 2022-03-09 00:00:00 I lzz
62+
63+
64+
delete from trips;
65+
----------------------------------------------------------------------------------------------------------------------------
66+
select * from trips where completed_date = '2022-02-28'::date - INTERVAL '3 DAY'; #this will give exactly 2 dates:
67+
68+
14 2022-02-25 00:00:00 I lzz
69+
15 2022-02-25 00:00:00 I lzz

0 commit comments

Comments
 (0)