Home > Software engineering >  Why is the equal sign not working in SQL, where as same dates are being extracted with greater than
Why is the equal sign not working in SQL, where as same dates are being extracted with greater than

Time:10-14

I want to extract customers created on 2021-10-14 (yyyy-mm-dd),

select * from customers where created_at = '2021-10-14'

This is giving 0 rows as output. Whereas this code is showing the data created on 2021-10-14:

select * from customers where created_at > '2021-10-14' and created_at < '2021-10-15'

why is equal to not working in the above query and why is greater than including the given date.

CodePudding user response:

Probably because your created_at column is of type datetime. And for instance a created time of

'2021-10-14 12:00:00' 

is not not the same as

'2021-10-14'

because it is actually comparing the time value too which is

'2021-10-14 00:00:00' 

The query you used (with the fixed typo of >=) is actually the correct way to go since it is able to use indexes for better performance

select * 
from customers 
where created_at >= '2021-10-14' 
  and created_at < '2021-10-15'
  • Related