Solved

Oracle Date comparison by concatenating 2 fields

Posted on 2007-04-10
4
771 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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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 about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now