Solved

Case statement based on time then between dates

Posted on 2011-03-24
6
262 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
[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
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35212392
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
ID: 35212398
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
ID: 35212418
You may need to replace GETDATE() with @getdate for testing (fyi)
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 35212420
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
ID: 35267601
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
ID: 35364481
Sharath123
I have managed to get this code working sufficiently for my needs thanks,
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a step to a system backup job 6 30
Search query matching words 20 38
Enabled trace flag 4135 or 4199 - SQL SERVER 2 22
Parsing this XML works but the other one doesn't 9 27
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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