Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

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

1834 Posts

Posted - 10/18/2012 :  12:22:19  Show Profile  Reply with Quote
   @dtEnd datetime =
      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

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

   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  
 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.02 seconds. Powered By: Snitz Forums 2000