Blog Post

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

,

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

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating