1

I have data like this:-

select id,created_at from tickets where created_at between '2020-01-23' and '2020-01-25';
 id  |         created_at         
-----+----------------------------
 297 | 2020-01-24 06:27:29.892841
 298 | 2020-01-24 06:57:51.710571
 299 | 2020-01-24 08:00:58.168503
 300 | 2020-01-24 08:41:27.063816

I want result to be filter by time i.e within that date range I want record created between 5 to 7

expected result:-

 id  |         created_at         
-----+----------------------------
 297 | 2020-01-24 06:27:29.892841
 298 | 2020-01-24 06:57:51.710571
0

3 Answers 3

1

Try casting the timestamp column to time and keep your logic.

CREATE TEMPORARY TABLE tickets ( id int, created_at timestamp);
INSERT INTO tickets VALUES
 (297 ,' 2020-01-24 06:27:29.892841'),
 (298 ,' 2020-01-24 06:57:51.710571'),
 (299 ,' 2020-01-24 08:00:58.168503'),
 (300 ,' 2020-01-24 08:41:27.063816');


SELECT id, created_at FROM tickets
WHERE created_at BETWEEN '2020-01-23' and '2020-01-25'
AND created_at::TIME BETWEEN '05:00:00' AND '07:00:00';

 id  |         created_at         
-----+----------------------------
 297 | 2020-01-24 06:27:29.892841
 298 | 2020-01-24 06:57:51.710571
(2 Zeilen)

Using extract(hour ...) is also an alternative, but you lose the ability to consider minutes in your time interval, e.g. 05:42:00-06:55:00

Sign up to request clarification or add additional context in comments.

Comments

0

You can run some query like this

select id,created_at 
from tickets 
where created_at between '2020-01-23' and '2020-01-25' 
and created_at::time between '#05:00:00#' and '#07:00:00#';

Comments

0

You can use Postgres' date function extract():

select id,created_at 
from tickets 
where 
    created_at between '2020-01-23' and '2020-01-25'
    and extract(hour from created_at) between 5 and 6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.