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 2000 Forums
 Transact-SQL (2000)
 Calculate Years in Service between 2 dates

Author  Topic 

hanifikri
Starting Member

14 Posts

Posted - 2007-02-15 : 21:58:46
Hi,

I want to calculate Years in Service between 2 dates.

Example:
Current Date: 16/02/2007
Employment Date: 01/10/1992

Years in Service = (Employment Date - Current Date)
The answer should be 14.++.

If i used datediff(year,employment date,current date) function i will get 15 years. This is not the right answer.

Please help.

Thanks.

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-02-15 : 23:10:27
You can use the function on this link:
Age Function F_AGE_IN_YEARS:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462


CODO ERGO SUM
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-15 : 23:10:42
[code]
datediff(year month, employment date, current date) / 12.0
[/code]


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-15 : 23:14:39
F_AGE_IN_YEARS returns INT. Think hanifikri wanted the year diff including the decimals


KH

Go to Top of Page

hanifikri
Starting Member

14 Posts

Posted - 2007-02-15 : 23:26:45
I get the answer that i want.

Thanks to both of you.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-02-15 : 23:29:17
quote:
Originally posted by khtan

F_AGE_IN_YEARS returns INT. Think hanifikri wanted the year diff including the decimals


KH





Hard to tell exactly what he wanted. If that is what he wants, the datediff you posted doesn't really do it either. For example:
select Years=datediff(month,'20070131','20070201')/12.0

Years
-------------------
.083333

But .0027397 would be closer to correct, 1/365.

In any case, what he asked for is "Years in Service between 2 dates", and F_AGE_IN_YEARS will return the whole number of years.







CODO ERGO SUM
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-16 : 00:55:36
quote:
I get the answer that i want.

Anyway, whatever he is asking, he got the answer he wanted.


KH

Go to Top of Page
   

- Advertisement -