5
I need to create a SQL function that returns the employee’s home time, example: years old, months and days
So far I’ve managed to bring the years old and months, but I couldn’t get the logic of day yet, someone can help me ?
DECLARE @DTINI DATE, @DTFIM DATE
SET @DTINI = '2015-12-17'
SET @DTFIM = '2016-03-10'
SELECT CAST(DATEDIFF(DAY,@DTINI,@DTFIM)/365.25 AS INT) ANOS,
CASE WHEN DATEDIFF(DAY,@DTINI,@DTFIM)/30 > 11 THEN 0
ELSE DATEDIFF(DAY,@DTINI,@DTFIM)/30 END MESES
PL-SQL in SQL Server? You are using SQL Server + T-SQL or Oracle + PL-SQL?
– Anthony Accioly
PL-SQL in SQL Server.
– Paulo Camargo
Paulo, are you sure? As far as my knowledge goes, no one has been able to port PL/SQL to SQL Server... Are you not using T-SQL without knowing? The code looks like T-SQL
– Anthony Accioly
Really, it’s a T-SQL, forgive me for the confusion.
– Paulo Camargo
Already asked and answered in Calculate Difference between Dates in SQL Server, with Years, Months and Days output
– Bacco