SQL Question.

I have a set of loan data where I have a Loan Date column. The dates mentioned there are all on or before 2012. I have to find out all the loan details of those loans which are more than one year old. How to find that out?
Soumi ChakrabortyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak ChauhanSQL Server DBACommented:
select LoanDate from Table where LoanDate <= (getdate()-365)
0
Soumi ChakrabortyAuthor Commented:
But GetDate() will give me the current date correct?
0
Soumi ChakrabortyAuthor Commented:
I want to use the DateDiff function between the oldest and the newest dates in the column. How to do that?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Ankit PareekOnline MarketingCommented:
select *
from MyTable
where MyDate > DATEADD(year, -1, GetDate())

Open in new window

Have you try above syntax  change year -1 to as you wish before or after as per date
0
Deepak ChauhanSQL Server DBACommented:
yes but please note the syntax getdate()-365,

365 (days)
0
Soumi ChakrabortyAuthor Commented:
There is my problem. If I use the getdate function, I will not get the desired results, because all the dates are older than 2013. So it will display all the results. I want to use the DateDiff function to find out the number of months between the oldest date and the date in each row.
0
Deepak ChauhanSQL Server DBACommented:
Ok .Try this.

declare @min datetime
set @min =(select min(loanDate) from TableName)
select loanDate, datediff(mm, @min, loanDate) as Diff_MONTH    from TableName
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Soumi ChakrabortyAuthor Commented:
This is a great solution. Worked for me.
0
Deepak ChauhanSQL Server DBACommented:
And this is for date difference between oldest and newest date. Now you have three queries hope this is what you need.

declare @min datetime
declare @max datetime
set @min =(select min(loanDate) from TableName)
set @max =(select max(loanDate) from TableName)

select loanDate, datediff(mm, @min, @max) as MONTH_DIFF    from TableName
0
Soumi ChakrabortyAuthor Commented:
The first one is much more appropriate. Thank you so much for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.