• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Case statement based on hour in timestamp

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
Spur
Asked:
Spur
  • 3
  • 3
1 Solution
 
Joel CoehoornDirector of Information TechnologyCommented:
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
 
Joel CoehoornDirector of Information TechnologyCommented:
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
 
SpurAuthor Commented:
Giving it a try now, thanks icoehoorn!
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Joel CoehoornDirector of Information TechnologyCommented:
Just noticed my enddate and startdate may be backwards also.
0
 
SpurAuthor Commented:
Thanks, worked great!
0
 
SpurAuthor Commented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now