Solved

Case statement based on hour in timestamp

Posted on 2008-06-19
6
213 Views
Last Modified: 2010-04-21
I have a timestamp with date and time in it. I want to assign a value to a record which depends on the time of day but the date is irrelevant. I need to write a case statement something like this:

case when (start time between 10 pm and 6 am) and (end time - start time is > 60) then do this
when end time - start time > 20 then do this
else do this end

I don't know the syntax to evaluate a timestamp which only looks at the hour.
0
Comment
Question by:Spur
[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
  • 3
  • 3
6 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 125 total points
ID: 21822186
CASE WHEN (DATEPART(hh, StartTime) < 6 OR DATEPART(hh, StartTime) > 22) AND DATEDIFF(mi, EndDate, StartDate) > 60 THEN
-- first condition
ELSE WHEN DATEDIFF(mi, EndDate, StartDate) > 20 THEN
-- 2nd condition
END
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21822201
Oops, I was thinking minutes for the 60 and 20 conditions.  Change that from 'mi' to 'hh' to use hours instead.  Though perhaps you meant for that to be minutes...
0
 

Author Comment

by:Spur
ID: 21822220
Giving it a try now, thanks icoehoorn!
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21822279
Just noticed my enddate and startdate may be backwards also.
0
 

Author Closing Comment

by:Spur
ID: 31468764
Thanks, worked great!
0
 

Author Comment

by:Spur
ID: 21822647
That part is easy, it is the datepart function I had never used or known about :) Works like a charm, Thanks jcoehoorn
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

759 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