Solved

How to compare a time in Oracle?

Posted on 2002-03-21
4
23,051 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
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 47

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now