0
Good morning. In the movement table, I have, among others, the date and time fields. I need to select between the chosen date and the following day (Ex.: between 09/11/2020 and 09/12/2020). I need the select Filtre between '07:00' of the day 11/09/2020 and '07:00' of the day 12/09/2020, informing the movements 24 hours.
Between dates, manually informing, ok:
select * from movimento
WHERE '[2020-09-11, 2020-09-12]'::daterange @> data
order by data,hora
However, I would like, tbm, to automatically pick up the day after the first informed day. In addition to the time filters. To know: In the system, the time is entered manually during the registration as it can be done outside the current time.
Postgresql in particular provides the following special date values (date and timestamp):
today
,tomorrow
andyesterday
considering, for timestamp, the time (00:00).– anonimo
Assuming your date and time fields are of the type
date
andtime
use:WHERE (data+hora) - INTERVAL '7 HOUR' BETWEEN 'today'::timestamp AND 'tomorrow'::timestamp;
– anonimo
I assumed that
data
be the typedate
andhora
of the kindtime
. Here is a test:bdteste=# SELECT '2020-09-11 12:00:00'::TIMESTAMP - INTERVAL '7 HOUR' BETWEEN 'today'::timestamp AND 'tomorrow'::timestamp;
 ?column? 
----------
 t
(1 row)
– anonimo
Thanks anonymity. I did as you suggest, but the return brings all the records. I need all records to appear between the chosen date (09/11/2020) and your time (set at 07:00:00 in the morning) and your next day (09/12/2020 07:00:00). Totaling 24 hours of movements.
– Jones713
My test worked as expected:
SELECT * FROM (VALUES ('2020-09-09', '10:00'), ('2020-09-10', '03:00'), ('2020-09-11', '12:00'), ('2020-09-12', '05:00'), ('2020-09-12', '18:00')) AS t(data, hora) WHERE (data::DATE + hora::TIME) - INTERVAL '7 HOUR' BETWEEN 'today'::timestamp AND 'tomorrow'::timestamp;
 data | hora 
------------+-------
 2020-09-11 | 12:00
 2020-09-12 | 05:00
(2 rows)
– anonimo
I managed to follow the anonymous tip. Thanks for the information.
– Jones713