Solved

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

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

773 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