Code sample below counts the number of days in a month. This function is a simple series of WHEN and ELSE constructs.
The function takes the number a date as an argument and returns an integer whose value is the number of days in the month the given date belongs to.
CREATE FUNCTION [dbo].[ufn_CountNoOfDaysInMonth] ( @pDate DATETIME ) RETURNS INT AS BEGIN
RETURN CASE WHEN MONTH(@pDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31 WHEN MONTH(@pDate) IN (4, 6, 9, 11) THEN 30 ELSE CASE WHEN (YEAR(@pDate) % 4 = 0 AND YEAR(@pDate) % 100 != 0) OR (YEAR(@pDate) % 400 = 0) THEN 29 ELSE 28 END END
END GO
|
No responses found. Be the first to respond and make money from revenue sharing program.
|