简体   繁体   中英

How can I compare date in oracle sql without using to_date /to_char function?

    select count(*) as a 
    from event 
    where eventName='ARTICLE' 
    and to_char(event_date,'DD-MM-YYYY')= '08-04-2016';

I want to compare date using a specific date format but when I run this query it is taking lot of time. Is there any way which I can get same result with minimum time?

Do you have an index on EVENT_DATE? If you don't then you've got a full table scan and that's as fast as it gets without building an index.

If you do have an index your query won't use it, because the to_char() will disable it. Let's assume your table has enough different dates and that they are sufficiently clumped together to make it worth using an index. You can change the query to use an index in a couple of ways.

If EVENT_DATE contains only the date element use this

where event_date = date '2016-04-08'

If EVENT_DATE contains a time element use this

where event_date >= date '2016-04-08'
and event_date < date '2016-04-09'

Note that Oracle stores DATE values in a special way. They are not stored with any particular mask. Formatting is just a display thing. So all we need to do is pass the target date in a meaningful format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM