Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Case statement based on time then between dates

Posted on 2011-03-24
6
Medium Priority
?
267 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Accepted Solution

by:
Sharath earned 2000 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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