Solved

Case statement based on time then between dates

Posted on 2011-03-24
6
258 Views
Last Modified: 2012-08-13
I am having problems with the correct syntax for the below query I have tried a number variations but inevitably SQL throws a dummy spit. Looking for suggestions to achieve the results I have written in longhand the first line of the query works fine. It returning the required records I have the issues with. You can use the table below if required as a test sample

create table Date_Sample(dt datetime,amount int)
insert into table1 select '01/26/2011 8:00', 3
insert into table1 select '01/26/2011 9:00', 2
insert into table1 select '01/26/2011 16:00', 2
insert into table1 select '01/26/2011 20:00', 6
insert into table1 select '01/25/2011 20:00', 3
insert into table1 select '01/25/2011 20:10', 1
insert into table1 select '01/25/2011 21:30', 2
insert into table1 select '01/25/2011 16:30', 2
insert into table1 select '01/25/2011 08:30', 2
insert into table1 select '01/25/2011 06:00', 2


declare @getdate datetime
set @getdate='1/26/2011 06:04'

select *

From test.dbo.Date_Sample
Where (CASE
WHEN @GetDate datepart(hh,@getdate)< 7 THEN
      Query the dt field for all records between 07:00 AM and 19:00 the day before
WHEN @Getday datepart(hh,@getdate) < 19 THEN
      Query the dt field for all records between 19:00 the day before to 7:00AM today
When @GetDate datepart (hh,@getdate) >=19 THEN
      Query the dt field for all records between 07:00 AM and 19:00 today      
0
Comment
Question by:OST-IS
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
Here's something to try:


declare @getdate datetime
set @getdate='1/26/2011 06:04'

select *
from test.dbo.Date_Sample
where (datepart(hh,@getdate)<7 and dt>=dateadd(hour,7,convert(datetime,convert(int,dt-1.5))) and dt<dateadd(hour,19,convert(datetime,convert(int,GETDATE()-1.5))) )
   OR (datepart(hh,@getdate)>=7 and datepart(hh,@getdate)<19 and dt>=dateadd(hour,19,convert(datetime,convert(int,dt-1.5))) and dt<dateadd(hour,7,convert(datetime,convert(int,GETDATE()-0.5))) )
   OR (datepart(hh,@getdate)>=19 and dt>=dateadd(hour,7,convert(datetime,convert(int,dt-0.5))) and dt<dateadd(hour,19,convert(datetime,convert(int,GETDATE()-0.5))) )

0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
re-posting as code ..
declare @getdate datetime
set @getdate='1/26/2011 06:04'

select *
from test.dbo.Date_Sample
where (datepart(hh,@getdate)<7 and dt>=dateadd(hour,7,convert(datetime,convert(int,dt-1.5))) and dt<dateadd(hour,19,convert(datetime,convert(int,GETDATE()-1.5))) )
   OR (datepart(hh,@getdate)>=7 and datepart(hh,@getdate)<19 and dt>=dateadd(hour,19,convert(datetime,convert(int,dt-1.5))) and dt<dateadd(hour,7,convert(datetime,convert(int,GETDATE()-0.5))) )
   OR (datepart(hh,@getdate)>=19 and dt>=dateadd(hour,7,convert(datetime,convert(int,dt-0.5))) and dt<dateadd(hour,19,convert(datetime,convert(int,GETDATE()-0.5))) )

Open in new window

0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
You may need to replace GETDATE() with @getdate for testing (fyi)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
try like this.
declare @getdate datetime
set @getdate='1/26/2011 06:04'
SELECT *
  FROM Date_Sample 
 WHERE (DATEPART(HH,@getdate) < 7 
        AND dt BETWEEN DATEADD(HH,7,CONVERT(DATETIME,CONVERT(DATE,DATEADD(dd,-1,@getdate)))) AND DATEADD(hh,19,CONVERT(DATETIME,CONVERT(DATE,DATEADD(dd,-1,@getdate)))))
        OR (DATEPART(HH,@getdate) BETWEEN 8 AND 18 
            AND dt BETWEEN DATEADD(HH,19,CONVERT(DATETIME,CONVERT(DATE,DATEADD(dd,-1,@getdate)))) AND DATEADD(hh,7,CONVERT(DATETIME,CONVERT(DATE,@getdate))))
        OR (DATEPART(HH,@getdate) >= 19 
            AND dt BETWEEN DATEADD(HH,19,CONVERT(DATETIME,CONVERT(DATE,@getdate))) AND DATEADD(hh,19,CONVERT(DATETIME,CONVERT(DATE,@getdate))))
/*
dt	amount
2011-01-25 16:30:00.000	2
2011-01-25 08:30:00.000	2
*/

Open in new window

0
 

Author Comment

by:OST-IS
Comment Utility
Sorry guys I have had one job after another stopping me getting to this. I have tried the suggestions and cannot get either to work for me. They tend to return the table OK just blank.
0
 

Author Comment

by:OST-IS
Comment Utility
Sharath123
I have managed to get this code working sufficiently for my needs thanks,
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

11 Experts available now in Live!

Get 1:1 Help Now