Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Diff and Leap Year 1

Status
Not open for further replies.

ibwebn65

Programmer
Sep 4, 2002
22
US
This is probably a simple question. I have been asked to get a list of accounts that have closed within five years of the account being open.

I have a table called ACCOUNTS that contains the following columns:

ACCOUNT_ID,
DATE_OPENED,
DATE_CLOSED

I'm probably on the wrong track, but I've been trying to use DATEDIFF to figure out the number of days between the account's open and closed dates. One thing I definitely do not understanding is how to account of leap years. A five year period could have either one or two leap years. Does anyone have any suggestions on what the SELECT statement would look like?

 
I think you can use the DATEADD function to just add 5 years to your starting value, and then compare your ending value to it. I've pasted a sample to illustrate:

if '01/28/2005' < dateadd(yy,5, '11/30/2000')
select 'It is less than 5 years'
else
select 'It is more than 5 years'

That will return less than 5 years.

Hope this helps
 
druer,

That looks like it will give me what I need. Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top