Solved

Date Comparison in Oracle

Posted on 2014-09-23
7
404 Views
Last Modified: 2014-09-30
Greeting,

I have a date field in Oracle. If I use TO_CHAR(My_date, 'YYYY-MM-DD HH24:MI:SS'), it gives me output like '2014-09-23 04:48:00'.

How to select the records which has My_date>='2014-09-23 10:00:00' and My_date<='2014-09-23 16:00:00' ?

Thanks.
0
Comment
Question by:mrong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40339927
use to_date to convert strings to dates.

select * from table_name
 My_date>=to_date('2014-09-23 10:00:00','YYYY-MM-DD H24:MI:SS') and My_date<=to_date('2014-09-23 16:00:00','YYYY-MM-DD H24:MI:SS')
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 200 total points
ID: 40339929
here:

where my_date >=to_Date('2014-09-23 10:00:00','YYYY-MM-DD HH24:MI:SS') and my_date<=('2014-09-23 16:00:00','YYYY-MM-DD HH24:MI:SS')

Open in new window

0
 

Author Comment

by:mrong
ID: 40339965
Try your suggestions but got the following error.

where my_date>=to_date('2014-02-13 17:00:00','YYYY-MM-DD H24:MI:SS')  
                                                                              *                                    
ERROR at line 2:
ORA-01821: date format not recognized
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 300 total points
ID: 40339971
Sorry.  Missed an 'H':
select * from table_name
 My_date>=to_date('2014-09-23 10:00:00','YYYY-MM-DD HH24:MI:SS') and My_date<=to_date('2014-09-23 16:00:00','YYYY-MM-DD HH24:MI:SS')
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40340039
and a 'where' :)

select * from table_name
where My_date>=to_date('2014-09-23 10:00:00','YYYY-MM-DD HH24:MI:SS') and My_date<=to_date('2014-09-23 16:00:00','YYYY-MM-DD HH24:MI:SS');

or:
(using between)

select * from table_name
where My_date between to_date('2014-09-23 10:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2014-09-23 16:00:00','YYYY-MM-DD HH24:MI:SS');
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 40340674
Remember to apply these "to_date" conversions to bind variables in your queries, and not to the database column values, or you cause a big performance penalty.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40341455
>>or you cause a big performance penalty.

To clarify what this piece means:
If a column has an index on it, wrapping that column in a function will not allow the index to be used.

Say you have an index on my_date.

This may use the index:
select count(*) from my_table where my_date >= to_date('01/01/2001','MM/DD/YYYY')
and my_date < to_date('01/02/2001','MM/DD/YYYY');

This will never use the index:
select count(*) from my_table where to_char(my_date,'MM/DD/YYYY') = '01/01/2001';

You 'can' create a function-based index with to_char(my_date,'MM/DD/YYYY') but most of the time this is unnecessary and a topic for another question.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question