[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

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'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
van-coug
Asked:
van-coug
  • 3
  • 2
  • 2
  • +1
1 Solution
 
knightEknightCommented:
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
 
knightEknightCommented:
oops, remove the ) after 'end' in the last line
0
 
van-cougAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
knightEknightCommented:
Sorry, I thought this was SQL Server, not Oracle.
0
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
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
 
awking00Commented:
See attached.
query.txt
0
 
van-cougAuthor Commented:
Thanks nav_kum_v!  That worked, much appreciated.
0
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
Good & Thanks
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now