March 15, 2010 at 11:59 am
Alvin Ramard (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
CirquedeSQLeil (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Anybody else see a problem with this statement?DECLARE @selectDate = getdate()-365
Yes, in SQL Server 2008 it is missing the data type. Should be:
DECLARE @selectDate datetime = getdate() - 365;
Agreed. As it is written in the article, you can do a getdate() - 365. Though there are different methods of achieving that.
I think y'all missed the point I was trying to make. I was referring to:
getdate() - 365 vs DATEADD(DAY, -365, GETDATE())
Those are equivalent.
I know they're equivalent, but I've always considered the first case to be bad practice.
Oh. The thing that stuck out a lot for me was the actual declaration of the variable.
I go back and forth between both methods - depending on mood.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 15, 2010 at 12:00 pm
I'm with Alvin. Wary of overloads on dates. Especially with potential calendar issues across time zones. I prefer using the functions that explicitly call for the operation
March 15, 2010 at 12:01 pm
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
CirquedeSQLeil (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Anybody else see a problem with this statement?DECLARE @selectDate = getdate()-365
Yes, in SQL Server 2008 it is missing the data type. Should be:
DECLARE @selectDate datetime = getdate() - 365;
Agreed. As it is written in the article, you can do a getdate() - 365. Though there are different methods of achieving that.
I think y'all missed the point I was trying to make. I was referring to:
getdate() - 365 vs DATEADD(DAY, -365, GETDATE())
Those are equivalent.
I know they're equivalent, but I've always considered the first case to be bad practice.
Now that I'll agree with!
Thank you 😎
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
March 15, 2010 at 12:02 pm
Steve Jones - Editor (3/15/2010)
I'm with Alvin. Wary of overloads on dates. Especially with potential calendar issues across time zones. I prefer using the functions that explicitly call for the operation
I wary of a change in settings, or a version upgrade, causing this to fail.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
March 15, 2010 at 12:04 pm
Alvin Ramard (3/15/2010)
CirquedeSQLeil (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Anybody else see a problem with this statement?DECLARE @selectDate = getdate()-365
Yes, in SQL Server 2008 it is missing the data type. Should be:
DECLARE @selectDate datetime = getdate() - 365;
Agreed. As it is written in the article, you can do a getdate() - 365. Though there are different methods of achieving that.
I think y'all missed the point I was trying to make. I was referring to:
getdate() - 365 vs DATEADD(DAY, -365, GETDATE())
What? They're both broken.
Either one fails every four years.
Edit: To be more precise, either one is wrong 1/4 of the time. Assuming it's meant to find the date one year ago.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 15, 2010 at 12:05 pm
Alvin Ramard (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
CirquedeSQLeil (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Anybody else see a problem with this statement?DECLARE @selectDate = getdate()-365
Yes, in SQL Server 2008 it is missing the data type. Should be:
DECLARE @selectDate datetime = getdate() - 365;
Agreed. As it is written in the article, you can do a getdate() - 365. Though there are different methods of achieving that.
I think y'all missed the point I was trying to make. I was referring to:
getdate() - 365 vs DATEADD(DAY, -365, GETDATE())
Those are equivalent.
I know they're equivalent, but I've always considered the first case to be bad practice.
Now that I'll agree with!
Thank you 😎
Well, I have learned something new.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 15, 2010 at 12:06 pm
Alvin Ramard (3/15/2010)
Anybody else see a problem with this statement?DECLARE @selectDate = getdate()-365
What I don't like about this is that it is attempting to subtract 1 year... and this will fail in leap years.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 15, 2010 at 12:06 pm
GSquared (3/15/2010)
Alvin Ramard (3/15/2010)
CirquedeSQLeil (3/15/2010)
Lynn Pettis (3/15/2010)
Alvin Ramard (3/15/2010)
Anybody else see a problem with this statement?DECLARE @selectDate = getdate()-365
Yes, in SQL Server 2008 it is missing the data type. Should be:
DECLARE @selectDate datetime = getdate() - 365;
Agreed. As it is written in the article, you can do a getdate() - 365. Though there are different methods of achieving that.
I think y'all missed the point I was trying to make. I was referring to:
getdate() - 365 vs DATEADD(DAY, -365, GETDATE())
What? They're both broken.
Either one fails every four years.
I have seen that one happen too. HAHAHA
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 15, 2010 at 12:10 pm
SQL Server 2008
declare @MyDate1 datetime,
@MyDate2 datetime2;
set @MyDate1 = getdate();
set @MyDate2 = @MyDate1;
select @MyDate1 - 10; -- Works
select @MyDate2 - 10; -- Fails
The error you get on the second one is like this one:
Msg 206, Level 16, State 2, Line 3
Operand type clash: datetime2 is incompatible with int
March 15, 2010 at 12:18 pm
Lynn Pettis (3/15/2010)
SQL Server 2008
declare @MyDate1 datetime,
@MyDate2 datetime2;
set @MyDate1 = getdate();
set @MyDate2 = @MyDate1;
select @MyDate1 - 10; -- Works
select @MyDate2 - 10; -- Fails
The error you get on the second one is like this one:
Msg 206, Level 16, State 2, Line 3
Operand type clash: datetime2 is incompatible with int
Thanks Lynn.
Now Timewarp.......
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 15, 2010 at 12:21 pm
Timewarp may be broken. I tried to use it last night to rewind the weekend and I could not get it to work. 🙁
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
March 15, 2010 at 12:27 pm
Timewarp? Redo the weekend? I don't think so. The Air Force Academy Fighting Falcons Hockey Team worked hard to beat the Black Knights of Army! They are on their way to the AHA semi-finals. They play Sacred Heart this week. Go Air Force!
😉
March 15, 2010 at 12:31 pm
I'm fine with going back to Friday evening.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 15, 2010 at 12:32 pm
You know ..... If the right "outsider" read the Thread, some of us might end up with free room and board for the rest of our lives, wall padding included. 😀 hmmm :w00t:
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
March 15, 2010 at 12:32 pm
Lynn only needs 4 more posts for 10,000 posts (not overall pts).
CountDown on?
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 15 posts - 12,661 through 12,675 (of 66,712 total)
You must be logged in to reply to this topic. Login to reply