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
624 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
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.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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 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…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

777 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