?
Solved

Simplest way to include data since a given point in time

Posted on 2011-02-17
19
Medium Priority
?
319 Views
Last Modified: 2012-05-11
Hi,

I need to return a set of results since a set time eg 9am.  

That 9am could be from yesterday (if we are pre 9am the following day) or 9am today.

Whats the cleanest method of performing this query?

Thanks in advance.

James.
0
Comment
Question by:JAMES
[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
  • 8
  • 6
  • 4
  • +1
19 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 34914691
WHERE HOUR(timestampfiled) >= 9
0
 
LVL 12

Expert Comment

by:Praveen Kumar
ID: 34914695
This function will return today's date, if current time is greater than 9am , else it will return yester day.

public DateTime ReturnTime()
   {
    DateTime dt;
   
   if(DateTime.Now.Hour > 9)
      dt=DateTime.Now;
   else
     dt =  DateTime.Adddays(-1);

    return dt;
     
   }
0
 

Author Comment

by:JAMES
ID: 34914701
OK - so this looks like I have to define a UDF and use that within my query.

I was hoping to get something which I could incorporate directly into my query.  Perhaps this isnt going to be possible.

0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 7

Expert Comment

by:mkobrin
ID: 34914747
what language are you writing in?
0
 

Author Comment

by:JAMES
ID: 34914760
LOL - Ah I see.  Thats a c# function.  Sorry this is SQL SERVER question which was the primary zone I posted it.  It went to c# too as thats our primary front-end.

My bad for not pointing that out on the original question! - SORRY!

0
 
LVL 7

Expert Comment

by:mkobrin
ID: 34914772
OK then what you want to do is not that hard.


                        DateTime myDate = DateTime.Now;
                        if (myDate.Hour > 9)
                            myDate = DateTime.Now.AddHours(9);
                        else
                            myDate = DateTime.Now.AddHours(-15);

Run your query with myDate as your param
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 34914776
oops sorry that should read:
                        DateTime myDate = DateTime.Now;
                        if (myDate.Hour > 9)
                            myDate = DateTime.Today.AddHours(9);
                        else
                            myDate = DateTime.Today.AddHours(-15);

0
 

Author Comment

by:JAMES
ID: 34914779
We are actually calling this query using xml web services from the c# front end through to the web service which then calls the SQL query and it's the time on the server that I need to use, not the clients time.

This whole thing has to be done in SQL Server im afraid.

Thanks for your thoughts anyway.
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 34914784
You can do the sam in SQL. Give me a few minutes and I'll write the code for you.
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 34914802
declare @datetime = getdate()
select @datetime = dateadd(hh,-datepart(hh,@datetime), @datetime)
select @datetime = dateadd(mi,-datepart(mi,@datetime), @datetime)
select @datetime = dateadd(ss,-datepart(ss,@datetime), @datetime)
if datepart(hh,getdate()) > 0
      select @datetime = dateadd(hh,9,@datetime)
ELSE
      select @datetime = dateadd(hh,-15,@datetime)

use @datetime as your parameter
0
 

Author Comment

by:JAMES
ID: 34914835
Not sure thats working.  Will test some more and get back to you.

Thanks so far.

James.
0
 
LVL 7

Assisted Solution

by:mkobrin
mkobrin earned 200 total points
ID: 34914867
Sorry I forgot that you can not set a variable when you declare it try this:

declare @datetime datetime
set @datetime = getdate()
select @datetime = dateadd(hh,-datepart(hh,@datetime), @datetime)
select @datetime = dateadd(mi,-datepart(mi,@datetime), @datetime)
select @datetime = dateadd(ss,-datepart(ss,@datetime), @datetime)
if datepart(hh,getdate()) > 0
      select @datetime = dateadd(hh,9,@datetime)
ELSE
      select @datetime = dateadd(hh,-15,@datetime)
select @datetime
0
 

Author Comment

by:JAMES
ID: 34914872
No, that wasnt the problem.  I corrected that but when I set a fixed date in to test it was always returning yesterday instead of today once the time went past 9am.

I will test further, may have been me.

0
 
LVL 18

Accepted Solution

by:
deighton earned 1800 total points
ID: 34914926
--the current time - set using GetDate() if you wish
declare @TARGET as datetime;
SET @TARGET = '02-17-2011 9:00:00';

--the hour of the day cut off time 
DECLARE @HOUR AS INT;
SET @HOUR = 9;

--calculate the cutoff time, e.g. the previous 9am
DECLARE @TIMECUTOFF as datetime;
select  @TIMECUTOFF = dateadd(hour,@hour,cast(cast(@TARGET as integer) - CASE  when (DATEPART(hour,@TARGET) < 9) THEN 1 ELSE 0 END as datetime));

--here is the value you can work with
SELECT @TIMECUTOFF;

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
ID: 34914950
blooper in the above when time is after midday - try the following


--the current time - set using GetDate() if you wish
declare @TARGET as datetime;
SET @TARGET = GetDate();

--the hour of the day cut off time 
DECLARE @HOUR AS INT;
SET @HOUR = 9;

--calculate the cutoff time, e.g. the previous 9am
DECLARE @TIMECUTOFF as datetime;
select  @TIMECUTOFF = dateadd(hour,@hour,cast(FLOOR(cast(@TARGET as float)) - CASE  when (DATEPART(hour,@TARGET) < 9) THEN 1 ELSE 0 END as datetime));

--here is the value you can work with
SELECT @TIMECUTOFF;

Open in new window

0
 

Author Closing Comment

by:JAMES
ID: 34914952
Perfect - thank you very much.

If you dont mind, I will award some points to mkobrin for his input also.

Thank you both.

James.
0
 

Author Comment

by:JAMES
ID: 34914959
So there was - lol

Ty again.
0
 
LVL 18

Expert Comment

by:deighton
ID: 34914990
the problem was that date converts to the nearest integer, so i converted to float and did a floor that should fix it.
0
 

Author Comment

by:JAMES
ID: 34914995
It does indeed and works great.  Ty.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

765 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