Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 DateDiff gives absolute date. how to get relative?

Author  Topic 

madlo
Starting Member

41 Posts

Posted - 2011-08-02 : 15:05:14
I have a query to get all the dates that are greater than five year
ie. When I user 2011/08/01 as criteria it should include 2016/08/02 but exclude 2016/08/01 However datediff(year,startdate,enddate) will always give 10 as it just looks at the year part. Is there an easy way to get the relative year.

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-08-02 : 15:13:38
Declare @dt datetime

Set @dt = '2011/08/01'

Select * From yourTable Where startDate > dateadd(year, 5, @dt)

Corey

I Has Returned!!
Go to Top of Page

himabindu.nvn
Starting Member

1 Post

Posted - 2011-08-02 : 19:02:50
select * from table where yearcolumn>dateadd(yy,-5,getdate())

I am new to sql programming. Please let me know if my understanding is right!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-03 : 01:07:50
quote:
Originally posted by himabindu.nvn

select * from table where yearcolumn>dateadd(yy,-5,getdate())

I am new to sql programming. Please let me know if my understanding is right!


it should be +5 not -5 as you want 5 years ahead

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madlo
Starting Member

41 Posts

Posted - 2011-08-04 : 07:30:06
This issue is the same as determining the age from the date of birth so I will just use the method described in those threads.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-04 : 08:43:58
quote:
Originally posted by madlo

This issue is the same as determining the age from the date of birth so I will just use the method described in those threads.


you mean this?

http://www.sqlteam.com/article/datediff-function-demystified

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -