Honestly there is small difference between the two methods, so use whichever turns your crank. I'd even hazard a guess that subtracting 30 might possibly be faster.
dvannoy,
Show us your WHERE clause.
-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
True, there is little difference between them. However dateadd is a handy function to know in case he later wants to go back by months, or years when getdate()-x doesn't work.
Denny
--Anything is possible. All it takes is a little research. (Me)
-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
Isn't getdate() a dd/mm/yyyy hh:mm:ss return? so if he's going back 30 days, he'll only get those items that are excactly that day, month, year, hour, minute and second, right?
Isn't dateadd(dd, -30, (cast(convert(char(8),getdate(),1) AS datetime))) going to get him a better match?
Sort of. To get a day match he would need to get the value for 30 days ago, convert to to just the date (the method you give will work fine), and convert the value within the field to the same format.
The reason for that is that when you conver to format 1 you strip off the time, but when it gets compaired against the datetime field in the table, it gets the time of midnight attached. So it's actually comapiring 11/06/2004 00:00:00.000 (getdate() is acurate to the thousandth of a second, as SQLBill shows above).
Denny
--Anything is possible. All it takes is a little research. (Me)
I am trying to get all records from today and 30 days ago regardless of what Date I run the query. I was using TABLE.DATE > '11/01/2004' just to make sure the query is returning records.
because as they are saying, all the datetimes in the database with various times throughout the day are NOT equal to 12am exactly, on the same day.
It's just like doing a query for numbers equal to 4 and then wondering why it's not selecting 4.2 and 4.5 and 4.773837483.
You have to do > 4 AND <= 5 to cover the range of 'times' between the two 'dates,' 4 and 5.
-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.