--How to Get total working experience of an employee if only Date of joining column exits in your table.
Create table emp_details(
DOJ smalldatetime -- Date of Joining
Insert into emp_details values('RAJA',convert(smalldatetime,'23/11/2002',103))
select * from emp_details
Declare @total_exp int
select @total_exp = DateDiff(year,DOJ,GETDATE()) from emp_Details where EmpName ='RAJA'
SELECT @total_Exp as total_Exp
(Note : you have to execute to all the codes exit within the ****** boundary.)
I have extended the DATEDIFF method to get exact difference in terms of months to find experience in number of years and months form.
Below is the modified code:
DECLARE @total_month INT, @year INT, @month INT;
SELECT @total_month = DATEDIFF(MONTH,DOJ,GETDATE()) FROM emp_Details WHERE EmpName ='RAJA';
SET @year = @total_month / 12;
SET @month = @total_month % 12;
PRINT 'Total experience is : ' + CONVERT(VARCHAR, @year) +' years and ' + CONVERT(VARCHAR, @month) + ' Months.';