Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have been facing a strange scenario when comparing dates in PostgreSQL(version 9.2.4 in windows). I have a column in my table say update_date with type 'timestamp without timezone'. Client can search over this field with only date (i.e: 2013-05-03) or date with time (i.e: 2013-05-03 12:20:00). This column has the value as the timestamp for all rows currently and has the same date part(2013-05-03) but the difference in time part.

When I'm comparing over this column, I'm getting different results. Like the followings:

select * from table where update_date >= '2013-05-03' AND update_date <= '2013-05-03' -> No results

select * from table where update_date >= '2013-05-03' AND update_date < '2013-05-03' -> No results

select * from table where update_date >= '2013-05-03' AND update_date <= '2013-05-04' -> results found

select * from table where update_date >= '2013-05-03' -> results found

My question is how can I make the first query possible to get results, I mean why the 3rd query is working but not the first one?

Can anybody help me with this? Thanks in advance.

1 Answer

0 votes
by (40.7k points)

It seems like you prefer the first form because you want to avoid date manipulation on the input string.

Use the below query: 

SELECT *

FROM table

WHERE update_date >= '2013-05-03'::date

AND update_date < ('2013-05-03'::date + '1 day'::interval);

To master SQL statements, queries and become proficient in SQL queries, enroll in our industry-recognized SQL course

Browse Categories

...