[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to Compare DateName(YEAR, TABLENAME) to NOW YEAR.......

Posted on 2008-06-12
8
Medium Priority
?
478 Views
Last Modified: 2012-06-27
DATABASE NAME:
HR_ASSOCIATES
TABLE NAME :
HireDate
COMMAND:
 (DATENAME(YEAR, HireDate)

Hello,
 im trying to build a report out of the HR DATABASE to allow our Users to Pull a Report of Associates Anniversary's and have it output the number of years the associate has been there.
any help would be greatly appreciated. the report is already working for the user to select the month and it will only show the people with anniversarys that month but I would love it to show the number of years as well........for example....if Table data Shows 2002, and Todays Year is 2008 how can i get SQL to Show me 6 YEARS?

Thanks,
PhatAdam
Information Systems
0
Comment
Question by:PhatAdam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 21770836
You can do it like this:
Select Year(GetDate()) - Year(YearOfHire)  
0
 
LVL 8

Expert Comment

by:i2mental
ID: 21770866
You could use the DATEDIFF function.

SELECT DATEDIFF (yy,'2/1/2002','6/12/2008')

returns 6
0
 

Author Comment

by:PhatAdam
ID: 21770946
right that would work....except the actual date we are starting with is in the table so it could be 2001 or 1999....the origianl date starts as whatever is in the SQL TABLE
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 16

Expert Comment

by:brad2575
ID: 21770976
You just add mine or i2mental's code examples to your existing select query.  You would just change the dates we used with dates from the tables.
0
 
LVL 6

Expert Comment

by:Jerryuk007
ID: 21770987
declare @yearjoin datetime
Set @yearjoin='2002/06/01' -- This can be extracted from your database
select datediff(yy,@yearjoin,getdate())as 'Number of year in the firm'

If you wish, the select above can be replaced with the one below (replace "hiredate_field" with the correct field name)

select datediff(yy,hiredate_field,getdate())as 'Number of year in the firm' from HR_ASSOCIATES.dbo.HireDate

Jerry
0
 
LVL 6

Expert Comment

by:Jerryuk007
ID: 21771059
Or you can do something like:

declare @yearjoin datetime
Set @yearjoin='2002/05/10' -- This can be extracted from your database
select rtrim(convert(char,datediff(yy,@yearjoin,getdate())))+' years and '+
rtrim(convert(char,datediff(mm,@yearjoin,getdate())-12*(convert(int,datediff(mm,@yearjoin,getdate()))/12)))+' Months' as 'Employment Length'

;-)

Jerry
0
 

Author Comment

by:PhatAdam
ID: 21771079
right but i cannot use SQL CODE...this is in reporting it has to be a Function Expression, doesnt it?
0
 
LVL 12

Accepted Solution

by:
jgv earned 2000 total points
ID: 21771735
If you have the option to modify the sql statement that pulls the report data from the database then use one of the above suggestions. If you don't have that option then you can use this expression in the report. Just replace "TheHireDateField" with the proper dataset field name.
=DateDiff("yyyy",Fields!TheHireDateField.Value, Date.Today)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question