Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>Select ... From Table Where DateCol &gt;= '20100303' And DateCol &lt; '20100314' And DatePart(hh,DateCol) Between 18 And 23 </code></pre> <p>Note that I use strictly less than in the comparison to 20100314 so it returns anything before 2010-03-14 midnight. </p> <p><strong>EDIT</strong> Realized you said PM and not AM. </p> <p><strong>ADDITION</strong> In SQL, the statement "Foo Between DateA And DateB" is translated into "Foo >= DateA and Foo &lt;= DateB". Notice the second part of that statement is less than <strong>or equal to</strong> to DateB. In our query, we want to include everything on 2010-03-13 all the way through midnight. I achieve that by restricting my search to values <strong>strictly less than</strong> the day after I want to search. </p> <p>With respect to the hour, DatePart(hh, DateVal) will return the hour of the day using the 24 hour clock. So 6 PM is really 18:00 hours. If we want between 6 PM and 11 PM we really want between 18:00 and 23:00.</p> <p><strong>ADDITION</strong> It occurs to me that there is a small problem in my original solution. The system will return values whose time is anywhere during the 11 o'clock hour (e.g. 11:01 PM, 11:30 PM, 11:50 PM etc.). Here is another solution that would solve that:</p> <pre><code>Select ... From Table Where DateCol &gt;= '20100303' And DateCol &lt; '20100314' And DateAdd(day, -DateDiff(Day, 0, [DateCol]), [DateCol]) Between '18:00' And '23:00' </code></pre> <p>Basically, I'm using the DateDiff and DateAdd functions to strip off the Date portion of the value and then compare against the values we want. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload