http://www.sqlservercentral.com/blogs/sql_awesomesauce/2011/12/19/select-getdate-30-as-this-actually-works/

Printed 2014/09/20 02:13AM

SELECT GetDate() – 30 AS [This actually works??];

2011/12/19

I just ran across this in a piece of code (that I didn’t write):

WHERE Thing >GETDATE()- 30

Aaaand it works. If you run the title of this blog post, you’ll get the date 30 days ago. I had no idea you could do this…I’m very much used to using DateAdd() for all my date manipulation needs.

The equivalent DateAdd() syntax, btw, would be

WHERE Thing  DATEADD(dd,-30,GETDATE())

In a search for “GetDate() – 30″, the first link to come up is a tek-tips.com forum conversation with Denny Cherry (web, Twitter):

dateadd would be a better way to handle this.
dateadd(dd, -30, getdate()).  Check BOL for full syntax.

…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

Well, that mostly answers that, then.  Has anyone seen any performance issues or inconsistent behavior with this particular syntax?  I don’t intend to switch, I’m just interested in the variation on the language.

Curiously,
Jen
http://www.MidnightDBA.com/Jen

Share on TwitterShare on LinkedInShare on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.