?
Solved

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

Posted on 2012-04-02
4
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

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 1500 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

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