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
607 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
'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 …
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…
Via a live example, show how to take different types of Oracle backups using RMAN.

707 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

16 Experts available now in Live!

Get 1:1 Help Now