SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Filtering Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AlonsoQ
Starting Member

Costa Rica
1 Posts

Posted - 10/18/2012 :  11:55:00  Show Profile  Reply with Quote
Hey everyone...

Im having some issues with a filter I need to add to one of my querys. I need to filter a table by date, basically what I need is to filter the results depending on the hour that the query is executed, for example if I run the query after 6pm, it should only display values from 6am to 6pm of today... but if I run the report anytime between 6am and 6pm of today, it should display values from 6pm of yesterday to 6am of today...

Please help :P :D

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1754 Posts

Posted - 10/18/2012 :  12:22:19  Show Profile  Reply with Quote
declare
   @dtEnd datetime =
   case
      when cast(GetDate() as TIME) < '6:00' then DateAdd(Hour, -6, dateadd(Day, DateDiff(Day, 0, GetDate()), 0))   -- 6pm yesterday
      when cast(GetDate() as TIME) < '18:00' then DateAdd(Hour, 6, dateadd(Day, DateDiff(Day, 0, GetDate()), 0))   -- 6am today
      else DateAdd(Hour, 18, dateadd(Day, DateDiff(Day, 0, GetDate()), 0))   -- 6pm today
      end

declare
   @dtStart datetime = dateadd(Hour, -12, @dtEnd) 

select
   <whatever>
from
   MyTable
where
   MyDatetime between @dtStart and @dtEnd


=================================================
We are far more concerned about the desecration of the flag than we are about the desecration of our land. -Wendell Berry

Edited by - Bustaz Kool on 10/18/2012 12:24:17
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000