Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle Date comparison by concatenating 2 fields

Posted on 2007-04-10
4
Medium Priority
?
794 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

670 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