Solved

SQL Time Comparisons

Posted on 2007-11-15
2
154 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
2 Comments
 
LVL 8

Accepted Solution

by:
k_rasuri earned 125 total points
Comment Utility
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
Comment Utility
This works perfectly.  Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now