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
617 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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35151122
Sorry, I thought this was SQL Server, not Oracle.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to recover a database from a user managed backup

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