Solved

Case statement based on time then between dates

Posted on 2011-03-24
6
260 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
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 40

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Copy Database Wizard 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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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