Hi,
Here is a simple solution:
SELECT * FROM @table
WHERE dateadd(hh, myHour, myDate)
BETWEEN dateadd( hh, @startHour, CAST('8/21/2004' as datetime) ) AND
dateadd( hh, @endHour, CAST('8/25/2004' as datetime))
ORDER BY myDate, myHour
[quoted text, click to view] "Rick" <no@spam.com> wrote in message
news:eI7LLM4hEHA.3928@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> Once again my sql ambitions exceed my training and I turn to you for
> assistance. I read somewhere years ago that it's best to store dates with
no
> times (00:00:00 actually) and then add an hour column. This works great
for
> our needs because we have a counter stores it's results in the table this
> way.
>
> My SELECT statement uses BETWEEN on the date columns and it works as
> expected, however I can't get the SELECT to start from X day at N hour to
Y
> day at N hour and everything in between. What gets returned is X and Y
> dates with the hours of N.
>
> See code below, and thank you in advance for your help.
>
> Rick
>
>
> DECLARE @table table (
> myDate datetime
> , myHour int
> );
> DECLARE @startHour INT;
> DECLARE @endHour INT;
> SELECT @startHour = 12;
> SELECT @endHour = 20;
>
> INSERT INTO @table (myDate, myHour) VALUES ('8/21/2004', 1);
> INSERT INTO @table (myDate, myHour) VALUES ('8/21/2004', 12);
> INSERT INTO @table (myDate, myHour) VALUES ('8/21/2004', 20);
> INSERT INTO @table (myDate, myHour) VALUES ('8/23/2004', 2);
> INSERT INTO @table (myDate, myHour) VALUES ('8/23/2004', 15);
> INSERT INTO @table (myDate, myHour) VALUES ('8/23/2004', 23);
>
>
> SELECT *
> FROM @table
> WHERE (myDate BETWEEN '8/21/2004' AND '8/25/2004')
> --AND myHour >= @startHour OR myHour <= @endHour /* no good */
> --AND myHour >= @startHour AND myHour <= @endHour /* no good */
> --AND (myHour BETWEEN @startHour AND @endHour) /* no good */
> ORDER BY myDate, myHour
>
>