Get Tenure of Employee in Org

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create function [dbo].[GetTenure] (@dateOfJoin datetime)
Returns Varchar(200)
AS
Begin
DECLARE @today datetime, @thisYearJoinDay datetime
DECLARE @years int, @months int, @days int
set @today = GETDATE()
set @thisYearJoinDay = DATEADD(year, DATEDIFF(year, @dateOfJoin, @today), @dateOfJoin)
set @years = DATEDIFF(year, @dateOfJoin, @today) - (CASE WHEN @thisYearJoinDay > @today THEN 1 ELSE 0 END)
set @months = MONTH(@today - @thisYearJoinDay) - 1
set @days = DAY(@today - @thisYearJoinDay) - 1
return cast(@years as varchar) +'-'+ cast(@months as varchar)+'-'+cast(@days as varchar)
--SELECT @years as years, @months as months, @days as days
End

Comments

Popular posts from this blog

Who Deleted and Drop the record in table

Generate Password

Key Board Shortcuts