Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Case statement based on hour in timestamp

Posted on 2008-06-19
Medium Priority
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.
Question by:Spur
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
LVL 18

Accepted Solution

jcoehoorn earned 500 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
LVL 18

Expert Comment

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

Author Comment

ID: 21822220
Giving it a try now, thanks icoehoorn!
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 18

Expert Comment

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

Author Closing Comment

ID: 31468764
Thanks, worked great!

Author Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

722 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