Solved

Simplest way to include data since a given point in time

Posted on 2011-02-17
19
315 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:Mamtha1982
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 50 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 450 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

749 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