1

I have table which has structure like this.

CREATE TABLE users (
    id serial NOT NULL,
    created_at timestamp NOT NULL
    )

I have more than 1000 records in this table. This is my first query.

1 query

select id,created_at from users where id in  (1051,1052)

enter image description here

This returns two rows which is correct as as expected. However when I use

2nd Query

select id,created_at from users where  created_at = '2020-06-28'

or

select id,created_at from users where  created_at = date '2020-06-28'

It returns nothing, this is not expected result as it should return two rows against it.

enter image description here

Similarly if I use this

3rd Query

select id, created_at from users where created_at between date '2020-06-28' and date '2020-06-28' 

It returns nothing however I think this should also return two rows.

enter image description here

While this

4th Query

select id, created_at from users where created_at between date '2020-06-28' and date '2020-06-29'

returns two rows. enter image description here

Show timezone returns correct timezong in which currently i am

enter image description here

I did`t understand this, why the results are different in 2nd, 3rd and 4th query. How can i get same result as of query 1 using 3rd query.

2 Answers 2

2

One single reason for all your queries is that you are comparing timestamp with date

in Query 2

You are comparing 2020-06-28 13:02:53 = 2020-06-28 00:00:00 which will not match so returning no records.

in Query 3

You are using between i.e. 2020-06-28 13:02:53 between 2020-06-28 00:00:00 and 2020-06-28 00:00:00 which will not match so returning no records.

in Query 4

You are using between i.e. 2020-06-28 13:02:53 between 2020-06-28 00:00:00 and 2020-06-29 00:00:00. Here both records are falling in those timestamps and you are getting the records

So you have to compare date values. As right operand is a date type value, you have to convert the left operand to date. try this

for 2nd Query

select id,created_at from users where  date(created_at) = '2020-06-28'

for 3rd Query

select id, created_at from users where date(created_at) between date '2020-06-28' and date '2020-06-28'

You should opt 3rd method if you want to compare a date range. For one day only you should use 2nd query.

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

Comments

0

Because what you are doing is:

test(5432)=# select '2020-06-28'::timestamp;
      timestamp      
---------------------
 06/28/2020 00:00:00

You are selecting for created_at that is exactly at midnight and there is none. The same thing when you do:

select id, created_at from users where created_at between date '2020-06-28' and date '2020-06-28'

You already corrected the mistake in your 3rd query in the 4th query:

select id, created_at from users where created_at between date '2020-06-28' and date '2020-06-29'

where you span the time from midnight of 06/28/2020 to midnight 06/29/2020

An alternate solution is:

create table dt_test(id integer, ts_fld timestamp);
insert into dt_test values (1, '07/04/2020 8:00'), (2, '07/05/2020 1:00'), (3, '07/05/2020 8:15');
select * from dt_test ;
 id |       ts_fld        
----+---------------------
  1 | 07/04/2020 08:00:00
  2 | 07/05/2020 01:00:00
  3 | 07/05/2020 08:15:00

select * from dt_test where date_trunc('days', ts_fld) = '07/05/2020'::date;
 id |       ts_fld        
----+---------------------
  2 | 07/05/2020 01:00:00
  3 | 07/05/2020 08:15:00

In your case:

select id, created_at from users where date_trunc('days', created_at) = '2020-06-28'::date;

2 Comments

what about query2. Why cant i get results with this. I think it should return two rows.
No you won't as I explained in the first example. created_at = '2020-06-28' becomes created_at = '06/28/2020 00:00:00' and there no such values to match.

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.