Solved

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

Posted on 2012-04-02
4
240 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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