Solved

Simplest way to include data since a given point in time

Posted on 2011-02-17
19
317 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

696 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