I am using SQL Server 2008.
I'm trying to write a fairly simple query that got complicated when asked to include the Years of Service based off the last day of the year.
This is the query I have:
SELECT Employee,LastName,FirstName,PRGroup,PRDept,EarnCode, CONVERT(CHAR(10), HireDate, 101) AS HireDate,DATEDIFF(year,HireDate,'2013') AS YOS
from PREH
where PRCo = '200'
and ActiveYN = 'Y'
order by YOS
This works great except for two things...I want to be able to allow the user to enter the last day of the year through a parameter and it only returns whole years and management wants years and months. For example: Charlie Justice is hired on 4/19/2006 so he will have 7.70 Years of Service on 12/31/2013.
I realize that I'm missing something, but date functions have NEVER, EVER been my strong suit and I'm stuck. Can someone please help because I have a deadline and I'm just shut down on this one.
The output for my query is:
Employee LastName FirstName PRGroup Pr Dept EarnCode HireDate YOS
1111 Justice Charlie 45 1000 130 04/19/2006 7
I want it to be:
Employee LastName FirstName PRGroup Pr Dept EarnCode HireDate YOS
1111 Justice Charlie 45 1000 130 04/19/2006 7.70
Thanks in advance for helping me out.
I'm trying to write a fairly simple query that got complicated when asked to include the Years of Service based off the last day of the year.
This is the query I have:
SELECT Employee,LastName,FirstName,PRGroup,PRDept,EarnCode, CONVERT(CHAR(10), HireDate, 101) AS HireDate,DATEDIFF(year,HireDate,'2013') AS YOS
from PREH
where PRCo = '200'
and ActiveYN = 'Y'
order by YOS
This works great except for two things...I want to be able to allow the user to enter the last day of the year through a parameter and it only returns whole years and management wants years and months. For example: Charlie Justice is hired on 4/19/2006 so he will have 7.70 Years of Service on 12/31/2013.
I realize that I'm missing something, but date functions have NEVER, EVER been my strong suit and I'm stuck. Can someone please help because I have a deadline and I'm just shut down on this one.
The output for my query is:
Employee LastName FirstName PRGroup Pr Dept EarnCode HireDate YOS
1111 Justice Charlie 45 1000 130 04/19/2006 7
I want it to be:
Employee LastName FirstName PRGroup Pr Dept EarnCode HireDate YOS
1111 Justice Charlie 45 1000 130 04/19/2006 7.70
Thanks in advance for helping me out.