Solved

How to find Weekend, AfterHours, and Normal details based on date column

Posted on 2012-04-02
4
241 Views
Last Modified: 2012-06-21
I'm using Oracle 10G:

 Category: ‘Weekend’ is 19:45 EST Friday to 7:00 EST Monday
‘After Hours’ is 19:45 EST to 7:00 EST next day on Mon, Tue, Wed, Thu
Otherwise is ‘Normal’

I'm using Datefield (occurreddate) which is VARCHAR datatype
0
Comment
Question by:Talitha30
  • 2
4 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 37803332
Is this what you need?

select cast(convert(date, getdate() ,101) as sysname) +' - ' + cast(DATENAME(weekday ,getdate()) as sysname)
0
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 37813909
I think you're going to want something like this - it looks a bit ridiculous, but seems to get the job done (though it's based on whatever the local time is - if you're dealing with multiple timezones, you'll have to take that into account as well):

select occureddate, 
	case when to_char(to_date(occurreddate), 'D') = 1 then 'Weekend'
	     when to_char(to_date(occurreddate), 'D') = 2 then 
			case when to_char(to_date'occurreddate), 'HH24:MI') <= '07:00' then 'Weekend'
			     when to_char(to_date(occurreddate), 'HH24:MI') >=  '19:45' then 'After-Hours'
				 else 'Normal'
			end
	     when to_char(to_date(occurreddate), 'D') IN (3, 4, 5) then 
			case when to_char(to_date(occurreddate), 'HH24:MI') <= '07:00' then 'After-Hours'
			     when to_char(to_date(occurreddate), 'HH24:MI') >=  '19:45' then 'After-Hours'
				 else 'Normal'
			end
	     when to_char(to_date(occurreddate), 'D') = 6 then 
			case when to_char(to_date(occurreddate), 'HH24:MI') >= '19:45' then 'Weekend'
			     when to_char(to_date(occurreddate), 'HH24:MI') <=  '07:00' then 'After-Hours'
				 else 'Normal'
			end
	     when to_char(to_date(occurreddate), 'D') = 7 then 'Weekend'
	end as TimeType
 from YourTable

Open in new window

0
 

Author Comment

by:Talitha30
ID: 37862330
This is what I used to get the answer I needed:

CASE
When To_Char(To_date(hp.Receiveddate, 'yyyy-mm-dd hh24:mi:ss'), 'Day') IN ('Friday', 'Monday') and to_number(to_char(To_date(hp.Receiveddate, 'yyyy-mm-dd hh24:mi:ss'),'HH24')) >= 20 OR to_number(to_char(To_date(hp.Receiveddate, 'yyyy-mm-dd hh24:mi:ss'),'HH24')) < 7 then 'Weekend'
When To_Char(To_date(hp.Receiveddate, 'yyyy-mm-dd hh24:mi:ss'), 'Day') IN ('Monday', 'Thursday') and to_number(to_char(To_date(hp.Receiveddate, 'yyyy-mm-dd hh24:mi:ss'),'HH24')) >= 20 OR to_number(to_char(To_date(hp.Receiveddate, 'yyyy-mm-dd hh24:mi:ss'),'HH24')) < 7 then 'After Hours'
Else 'Normal' end category
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 37862420
I think you're just doing the same thing in your case that I'm doing in the one I posted earlier, but in a more condensed (and I think harder to read) format. Was there something wrong with the snippet I provided?
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

920 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

17 Experts available now in Live!

Get 1:1 Help Now