Solved

Simplest way to include data since a given point in time

Posted on 2011-02-17
19
278 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now