Solved

How to compare a time in Oracle?

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

Question has a verified solution.

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

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

821 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