I want the function to return age of the employee in specified date passed to the function by parameter
DROP FUNCTION IF EXISTS dbo.f_Employee_Age;
GO
CREATE FUNCTION f_Employee_Age(@date DATE)
RETURNS INT
AS
BEGIN
RETURN (SELECT DATEDIFF(yy, BirthDate, @date) FROM Employee)
END;
GO
SELECT dbo.f_Employee_Age('2012-12-21')
FROM Employee
WHERE FirstName LIKE 'Andrew';
When I try to do it without the function it works perfectly fine
SELECT DATEDIFF(yy, BirthDate, '2012-12-21')
FROM Employee
WHERE FirstName LIKE 'Andrew';
But if I pass the date parameter to a function I get this error:
Msg 512, Level 16, State 1, Line 12
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.