Solved

Case statement based on time then between dates

Posted on 2011-03-24
6
265 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

622 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