Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Time Comparisons

Posted on 2007-11-15
2
Medium Priority
?
168 Views
Last Modified: 2010-04-21
I have a query that I need to produce that gives the mealperiod that a particular order was produced in.  (Breakfast, Lunch, Dinner).  

This is so that my report can be exported into Excel and grouped/manipulated/massaged, etc.

However, nothing do seems to be able to get anything other than 'Dinner' as a result.
(possibly irrelevant note:  I don't care about the date, even though SQL forces me to store one... all I care about is the TIME portion of the 'TargetDelivery' field to make a determination of category)

Many thanks in advance.

Jay
----------------------------------------------------------------
Case 
        When CONVERT(DATETIME, OrderEntry.TargetDelivery, 108) <= CONVERT(DATETIME, '10:00:00 AM',8) Then 'Breakfast' 
 
When CONVERT(DATETIME, OrderEntry.TargetDelivery, 108) > CONVERT(DATETIME, '10:00:00 AM',8) AND CONVERT(DATETIME, OrderEntry.TargetDelivery, 8) < CONVERT(DATETIME, '3:00:00 PM',8) Then 'Lunch' 
 
Else 'Dinner' 
End As 'Meal Period', 
 
-------------------------------------------------------------------------

Open in new window

0
Comment
Question by:DataWedge
[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 Comments
 
LVL 8

Accepted Solution

by:
k_rasuri earned 500 total points
ID: 20291369
your query wont give the proper results as you cannot just convert 10:00am to date. instead use datpart something like this

select case when datepart(hh,getdate())<11 then 'Breakfast'
                    when datepart(hh, getdate()) between 11 and 18 then 'Lunch'
                   else 'Dinner' end
0
 

Author Closing Comment

by:DataWedge
ID: 31409391
This works perfectly.  Thanks.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this article, we’ll look at how to deploy ProxySQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

604 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