Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to compare a time in Oracle?

Posted on 2002-03-21
4
Medium Priority
?
25,848 Views
Last Modified: 2012-06-21
Hi,

I need to retrieve record from the Oracle database by comparing with a time range? The column "OTime" is of Date datatype. I'm using ASP.

This is my SQL:
"SELECT * FROM Purchase WHERE (OTime BETWEEN to_date('" & from_time & "', 'HH24:MI:SS') AND to_date('"& to_time &"', 'HH24:MI:SS'))

There are no errors, but no records were retrieved. I have only stored the time into "OTime". Do I need to store the date as well?

Thanks!

BearT
0
Comment
Question by:bearT
[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
4 Comments
 
LVL 7

Expert Comment

by:Bigfam5
ID: 6885241
Try something like this

Select * from Purchase
where to_date(to_char(OTIME,'hh24miss'),'hh24miss')
            between to_date('070000','hh24miss') and to_date('080000','hh24miss')

0
 
LVL 48

Expert Comment

by:schwertner
ID: 6885243
DATE type in Oracle allways stores date and time. It seems that the SYSDATE is stored as date component.

Try

"SELECT * FROM Purchase WHERE (to_date(OTime,'HH24:MI:SS') BETWEEN to_date('" & from_time & "', 'HH24:MI:SS') AND to_date('"&
to_time &"', 'HH24:MI:SS'))
0
 
LVL 6

Accepted Solution

by:
venkotch earned 300 total points
ID: 6885275
For the datatype of DATE Oracle always stores both date and time. If you need just the date, you can use trunc() function to put the time at midnight, but you have not an option to store only the time. So I'm wordering how you did it? However, thinking that in OTime field actually you have a datetime, you might be able to solve the problem.
Using to_date() with format mask of 'HH24:MI:SS' will be interpreded as SYSDATE with the time specified. However, I think your query will return error, because from time and to time parameters are not correctly used. The query should look like:

SELECT * FROM Purchase
 WHERE (OTime BETWEEN to_date('&from_time', 'HH24:MI:SS')
          AND to_date('&to_time', 'HH24:MI:SS'))

and thinking that we have the date included, to ignore it we may use:

SELECT * FROM Purchase
 WHERE (to_char(OTime,'HH24MISS') BETWEEN '&from_time'
        and '&to_time')

I guess this is not only the solution and to_char() will stop oracle to use any indexes of OTime column, except you have version >=8 and can use index based on function.
Good Luck.
0
 
LVL 9

Expert Comment

by:konektor
ID: 6885830
a variable of date datatype allways holds day/month/year information
so if you
DECLARE
  vTime DATE;
BEGIN
  SELECT to_date('13:27:11','HH24:MI:SS') INTO vTime FROM dual;
END;
vTime is this day, time 13:27:11, today 21-mar-2002 13:27:11

so if you want to compare only time you should convert it into number or char and compare chars
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
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.

609 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