Solved

Trying to pull records from a date field based on a time; if the date_scheduled falls between specific hours want to show the range

Posted on 2011-03-16
9
635 Views
Last Modified: 2013-12-07
I'm trying to pull records from a date field based on a time; if the date_scheduled falls between specific hours want to show the range. I tried a case statement, but cant get it to work. Do I need to use different functions within the case statemen, or is my syntax bad. Below is how I tried to do it. Going against oracle db.

(case date_scheduled when (to_char(date_scheduled, 'hh24') >= 05 and to_char(date_scheduled, 'hh24') < 07) then '05:00-07:00'
                       when (to_char(date_scheduled, 'hh24') >= 07 and to_char(date_scheduled, 'hh24') < 09)then '07:00-09:00'
                       when (to_char(date_scheduled, 'hh24') >= 09 and to_char(date_scheduled, 'hh24') < 11)then '09:00-11:00'
                       when (to_char(date_scheduled, 'hh24') >= 11 and to_char(date_scheduled, 'hh24') < 13)then '11:00-13:00'
                       when (to_char(date_scheduled, 'hh24') >= 13 and to_char(date_scheduled, 'hh24') < 15)then '13:00-15:00'
                       when (to_char(date_scheduled, 'hh24') >= 15 and to_char(date_scheduled, 'hh24') <= 17)then '13:00-15:00'
                       when (to_char(date_scheduled, 'hh24') < 05) then 'Before Hours'
                       when (to_char(date_scheduled, 'hh24') > 17)then 'After Hours')as "Time Range"
                       end),
0
Comment
Question by:van-coug
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35150998
case date_scheduled   when HOUR(date_scheduled) in (5,6) then '05:00-07:00'
                       when HOUR(date_scheduled) in (7,8) then '07:00-09:00'
                       when HOUR(date_scheduled) in (9,10) then '09:00-11:00'
                       when HOUR(date_scheduled) in (11,12) then '11:00-13:00'
                       when HOUR(date_scheduled) in (13,14) then '13:00-15:00'
                       when HOUR(date_scheduled) in (15,16) then '15:00-17:00'
                       when HOUR(date_scheduled) <  5 then 'Before Hours'
                       when HOUR(date_scheduled) > 17 then 'After Hours'
                       end) as 'Time Range',
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35151009
oops, remove the ) after 'end' in the last line
0
 

Author Comment

by:van-coug
ID: 35151068
Thanks knightEknight:

I tried it, but I'm getting an ORA-00905 missing keyword error- it highlighting the first 'in'.

select w.date_scheduled,
(case date_scheduled   when HOUR(date_scheduled) in (5,6) then '05:00-07:00'
                       when HOUR(date_scheduled) in (7,8) then '07:00-09:00'
                       when HOUR(date_scheduled) in (9,10) then '09:00-11:00'
                       when HOUR(date_scheduled) in (11,12) then '11:00-13:00'
                       when HOUR(date_scheduled) in (13,14) then '13:00-15:00'
                       when HOUR(date_scheduled) in (15,16) then '15:00-17:00'
                       when HOUR(date_scheduled) <  5 then 'Before Hours'
                       when HOUR(date_scheduled) > 17 then 'After Hours'
                       end) as 'Time Range',
w.work_order_id
from oms.work_orders w
where w.work_order_id > trunc(sysdate)
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 33

Expert Comment

by:knightEknight
ID: 35151122
Sorry, I thought this was SQL Server, not Oracle.
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 total points
ID: 35153593
try this. This works fine for me in oracle.

select w.date_scheduled,
case
when to_number(to_char(date_scheduled,'HH24')) < 5 then 'Before Hours'
when to_number(to_char(date_scheduled,'HH24')) >=5 and to_number(to_char(date_scheduled,'HH24')) < 7 then '05:00-07:00'
when to_number(to_char(date_scheduled,'HH24')) >=7 and to_number(to_char(date_scheduled,'HH24')) < 9 then '07:00-09:00'
when to_number(to_char(date_scheduled,'HH24')) >=9 and to_number(to_char(date_scheduled,'HH24')) < 11 then '09:00-11:00'
when to_number(to_char(date_scheduled,'HH24')) >=11 and to_number(to_char(date_scheduled,'HH24')) < 13 then '11:00-13:00'
when to_number(to_char(date_scheduled,'HH24')) >=13 and to_number(to_char(date_scheduled,'HH24')) < 15 then '13:00-15:00'
when to_number(to_char(date_scheduled,'HH24')) >=15 and to_number(to_char(date_scheduled,'HH24')) <= 17 then '13:00-15:00'                      
when to_number(to_char(date_scheduled,'HH24')) > 17 then 'After Hours' end as "Time Range",
w.work_order_id
from oms.work_orders w
where w.work_order_id > trunc(sysdate)

Thanks
0
 
LVL 32

Expert Comment

by:awking00
ID: 35156695
See attached.
query.txt
0
 

Author Closing Comment

by:van-coug
ID: 35157910
Thanks nav_kum_v!  That worked, much appreciated.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 35161825
Good & Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

730 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