x

how to create time restriction

I'm trying to create a daily report and I need some kind of date control. I want it to show only yesterday 11am to today 11am, so between those two times only. Thanks because I'm quite stuck.


SELECT *
   DATEADD(d,DATEDIFF(d,0,GETDATE()), 11:00:00.000'),                    
   DATEADD(d,DATEDIFF(d,0,GETDATE()), 11:00:00.000')-1                    
FROM Table1
WHERE name = "John"
ORDER BY *
go
more ▼

asked Nov 13 '09 at 11:10 AM in Default

garwend gravatar image

garwend
4 1 1 1

(comments are locked)
10|1200 characters needed characters left

4 answers: sort voted first

If I understand the question, you are almost there. You are trying to query data in a table between 11am yesterday and 11am today, correct? Then, just move your "Date" columns to the where clause:

SELECT *

DATEADD(d,DATEDIFF(d,0,GETDATE()), 11:00:00.000'),
DATEADD(d,DATEDIFF(d,0,GETDATE()), 11:00:00.000')-1 FROM Table1

WHERE name = "John" AND datefield between DATEADD(d,DATEDIFF(d,0,GETDATE()), 11:00:00.000') and
DATEADD(d,DATEDIFF(d,0,GETDATE()), 11:00:00.000')-1 ORDER BY *

go

more ▼

answered Nov 13 '09 at 11:36 AM

Scott 2 gravatar image

Scott 2
36

I guess the only other thing I need is to create a date field. Thanks! I knew I was close!
Nov 13 '09 at 12:34 PM garwend
You know this solution doesn't work? If it did (by switching place for the two datetime values), you would also include the 11:00 value for today?
Nov 17 '09 at 02:21 AM Peso
I recommend using "Day" instead of "D" for the first parameter to improve readability - e.g. DATEADD(Day, DATEDIFF(Day, ... - for example, do you know off the top of your head whether "m" would be Month? Minute? or Millisecond?
Nov 17 '09 at 07:56 AM Kristen ♦
(comments are locked)
10|1200 characters needed characters left

This is the proper way to write your query. It allows a present index to be used.

SELECT *
FROM Table1
WHERE Col1 >= DATEADD(DAY, DATEDIFF(DAY, 1, GETDATE()), '11:00')
AND Col < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), '11:00')
more ▼

answered Nov 16 '09 at 06:43 PM

Peso gravatar image

Peso
1.6k 5 6 8

(comments are locked)
10|1200 characters needed characters left

The suggestion above will NOT work.
BETWEEN operator need the two parameter in ASCENDING order, not descending order.
Replace the parameters and you're set.

more ▼

answered Nov 16 '09 at 08:39 AM

Peso gravatar image

Peso
1.6k 5 6 8

This is the reason why voting system fails if OP is allowed to select the correct answer
Nov 17 '09 at 09:32 AM Madhivanan
(comments are locked)
10|1200 characters needed characters left

Peso, I realized that it didn't work. Can you fix what is written above because I don't completely understand what you are saying. I'm pretty new to sql. also at the top of the query, I added: DECLARE @starttime as datetime SET @starttime

Does that make it better?

more ▼

answered Nov 16 '09 at 12:04 PM

garwend gravatar image

garwend
4 1 1 1

Please use "Add comment" rather than adding an Answer, thanks!
Nov 17 '09 at 07:58 AM Kristen ♦
Use the query that Peso posted and see if you get the correct answer
Nov 17 '09 at 09:30 AM Madhivanan
It worked! Thank you so much for your help everyone and Kristen, sorry that I didn't use the "add comment" feature. Thanks again you guys are amazing!
Nov 17 '09 at 10:33 AM garwend
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1933
x91
x28

asked: Nov 13 '09 at 11:10 AM

Seen: 1694 times

Last Updated: Nov 17 '09 at 07:59 AM