Solved

How to compare a time in Oracle?

Posted on 2002-03-21
4
24,827 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 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
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 how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

729 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