Solved

Date Comparison in Oracle

Posted on 2014-09-23
7
397 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

820 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