May 11, 2005 at 11:41 am
Hello,
This seems like a simple task, but need some help.
I need to set a column's date value to dates 90 days ago from today so something like:
GETDATE() - 90 DAYS
What's the easiest way to do this?
Thanks.
JN
May 11, 2005 at 11:45 am
I think I solved my own issue by posting here...
GETDATE() - 90 works just fine giving me 2/10/05, which is 90 days ago!
JN
May 11, 2005 at 11:48 am
You're better off learning this method :
Select dateadd(d, -90, Getdate())
May 11, 2005 at 11:52 am
If you can spare some time... why is that the chosen method, though both gives exact result including milliseconds?
Thanks.
JN
May 11, 2005 at 12:01 pm
Because you can add many more things than days (from books online)
Syntaxe
DATEADD ( datepart , number, date )
Year : aa, aaaa
quarter : qq, q
Month : mm, m
dayofyear : ja, a
Jour : jj, j
Week : wk, ww
Hour : hh
minute : min
second : ss, s
millisecond : ms
Also if the method used to store dates change from one version to the next your substraction may yield incorrect results.
May 11, 2005 at 10:49 pm
Don't forget that just subtracting 90 days using either method will not take you to midnight of the given day... the current time element will be included unless you do something like...
DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)-90
...or...
DATEADD(dd,DATEDIFF(dd,0,GETDATE()-90),0)
...or to exemplify Remi's method...
DATEADD(dd,-90,DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0))
Again, Remi's method would allow for other calculations very easily just by changing the first "dd" (and the value that follows) to the date part you wanted to add or subtract.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 12, 2005 at 3:12 am
Are you really looking for 90 days or are you looking for 3 months? Just in case you're looking for the latter, subtracting 90 days is not always exact. That's where DATEADD comes in really handy, since SQL Server will automatically deal here with the potential issues.
Just my $0.02 cents anyway.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 12, 2005 at 5:29 am
That's a pretty good two cents... lot's of folks forget that there IS a difference between 90 days and 3 months.
JN, here's the code without the time stripping part... that's what Remi meant about how useful the DATEADD function can be...
DATEADD(mm,-3,GETDATE())
Here it is with the time stripper...
DATEADD(mm,-3,DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0))
--Jeff Moden
Change is inevitable... Change for the better is not.
May 12, 2005 at 9:36 am
Lots of good suggestions! Thanks, guys! It did cross my mind that 90 days <> 3 months, but the person giving this spec to me wasn't too clear so I left it at 90 days, but now that I got a better suggestion, I'll change it just to be on the safe side.
Thanks, again, for your input.
JN
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply