Solved

Oracle Date comparison by concatenating 2 fields

Posted on 2007-04-10
4
767 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]
Comment Utility
what data type are the 2 fields DEPDATE and DESPTIME ?
0
 
LVL 1

Author Comment

by:wheelibin
Comment Utility
Hi, they are both DATE fields
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
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.

Join & Write a Comment

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…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

7 Experts available now in Live!

Get 1:1 Help Now