Istudent Rana

Istudent Rana

  • 1.4k
  • 342
  • 84k

how do i return 0 from function when query return no record

Oct 12 2018 11:05 AM
I have a function which return total hours worked. But in some cases there will be no entry at all. In that case select query return empty records. I would like to return 0 if there is no record. How do i achieve this?

ALTER FUNCTION [dbo].[ufnGetTotalHoursWorked](@id int)
RETURNS decimal(18,2)
AS
BEGIN
DECLARE @workedHours decimal(18,2);
SELECT @workedHours = (SELECT Sum(WorkedHours) FROM [WorkedHours] WHERE EmployeeId = @id GROUP BY EmployeeId having EmployeeId=@id)
RETURN @workedHours;
END

Answers (1)