Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date Comparison in Oracle

Posted on 2014-09-23
7
Medium Priority
?
411 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 800 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1200 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

721 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