CREATE FUNCTION [dbo].[ufn_GetDaysInYear] ( @pDate DATETIME )
RETURNS INT
AS
BEGIN
DECLARE @IsLeapYear BIT
SET @IsLeapYear = 0
IF (YEAR( @pDate ) % 4 = 0 AND YEAR( @pDate ) % 100 != 0) OR
YEAR( @pDate ) % 400 = 0
SET @IsLeapYear = 1
RETURN 365 + @IsLeapYear
END
GO
Description
The function above first determines if it is a leap year using the conditions
mentioned above. If it is a leap, a bit flag is set to 1, otherwise it
remains 0 as initially set. Then this bit flag is added to 365 and is
returned by the function.