Friday, February 17, 2012

Date calculation

Im relatively new with sql and im having a problem
creating a criteria based on todays date less 150 days.
I tried to use the following...
< GetDate () -150 however it will not let me use this.
I know that there must be away to do this, but I need
help.. Thank you in advance
On Tue, 3 Aug 2004 10:37:13 -0700, Bob wrote:

>Im relatively new with sql and im having a problem
>creating a criteria based on todays date less 150 days.
>I tried to use the following...
> < GetDate () -150 however it will not let me use this.
>I know that there must be away to do this, but I need
>help.. Thank you in advance
Hi Bob,
Try it with DATEADD(day, -150, getdate()).
If you remove the space between getdate and (), your code would work as
well, but I recommend using DATEADD anyway - just subtracting 150 relies
on the internal storage of datetime values; this is not guaranteed to
remain unchanged between SQL Server versions.
Also remember that getdate() gives current date *AND* time. Not a problem
for "smaller than" comparissons, but if you ever have to match on "equal"
date, you'll have to use some extra tricks to get rid of the time portion.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)

No comments:

Post a Comment