Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter by date in oracle SQL

I have a table with a date column (DD/MM/YYYY hh:mm:ss) as follows:

02/09/2021 09:50:37
03/09/2021 09:20:27
02/09/2021 14:05:25
03/09/2021 12:50:28
02/09/2021 14:25:26

What’s the most efficient way to filter by date?

Note: I tried with

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select date 
from table
where date = '02/09/2021'

However, this provides results that wrongly lack the time and only contain the date 02/09/2021.

For example, 02/09/2021 09:50:37 would not be returned.

>Solution :

A simple option is

select * 
from your_table
where trunc(date_column) = date '2021-09-02'

If there’s an index on date_column, it wouldn’t be used in that case so performance might suffer so you’ll have to either create a function-based index, or use a different where clause:

where date_column >= date '2021-09-02' and date_column < date '2021-03-03'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading