Solved

Oracle Date comparison by concatenating 2 fields

Posted on 2007-04-10
4
774 Views
Last Modified: 2013-12-19
Hi

I am trying to do a date comparison in an SQL select statement on Oracle 9i.  The problem in this particular case is that the date is stored in a field only containing the date and the time is stored in a field with a zero  date e.g. "00/00/00 12:01:01".

This is basically what I am trying to do:
...
AND G16.DESPDATE         >= to_date('27/03/2007 17:00','DD/MM/YYYY HH24:MI')  
AND G16.DESPDATE         <= to_date('28/03/2007 23:45','DD/MM/YYYY HH24:MI')  
...

G16.DESPDATE as I said contains only the date
G16.DESPTIME however contains the zero date AND the time value.


Does anyone know how I can go about concatenating the 2 fields to do this comparison?

Any help would be massively appreciated, I have already spent AGES on this!
0
Comment
Question by:wheelibin
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18883991
what data type are the 2 fields DEPDATE and DESPTIME ?
0
 
LVL 1

Author Comment

by:wheelibin
ID: 18887984
Hi, they are both DATE fields
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18888072
ok, here we go:
AND G16.DESPDATE         >= to_date('27/03/2007 17:00','DD/MM/YYYY HH24:MI')  
AND G16.DESPDATE         <= to_date('28/03/2007 23:45','DD/MM/YYYY HH24:MI')  
AND to_date( to_char( G16.DESPDATE, 'YYYY-MM-DD') || ' ' || to_char( G16.DESPTIME , 'HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') >= to_date('27/03/2007 17:00','DD/MM/YYYY HH24:MI')  
AND to_date( to_char( G16.DESPDATE, 'YYYY-MM-DD') || ' ' || to_char( G16.DESPTIME , 'HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') <=to_date('28/03/2007 23:45','DD/MM/YYYY HH24:MI')  
0
 
LVL 1

Author Comment

by:wheelibin
ID: 18888145
Thanks for your quick answer.....your example gives me "Invalid Number" which can only be because of the zero date.  If I select this "to_char( G16.DESPTIME , 'HH24:MI:SS')," it returns "00:00:00".

I have solved it by using your code but adding 366 to G16.DESPTIME (which should make the date 01/01/01).

This is now working:
AND G16.DESPDATE         >= to_date('27/03/2007','DD/MM/YYYY')  
AND G16.DESPDATE         <= to_date('28/03/2007','DD/MM/YYYY')  
AND to_date( to_char( G16.DESPDATE, 'YYYY-MM-DD') || ' ' || to_char( G16.DESPTIME +366 , 'HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') >= to_date('27/03/2007 17:00','DD/MM/YYYY HH24:MI')  
AND to_date( to_char( G16.DESPDATE, 'YYYY-MM-DD') || ' ' || to_char( G16.DESPTIME +366, 'HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') <=to_date('28/03/2007 23:45','DD/MM/YYYY HH24:MI')  

Thanks a lot for that
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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.

810 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