Is_Leap Function

,

Example 1:

After creating the function copy this into your Query Pane

SELECT
dbo.Is_Leap (1800) AS [1800],
dbo.Is_Leap (1900) AS [1900],
dbo.Is_Leap (2000) AS [2000],
dbo.Is_Leap (2004) AS [2004],
dbo.Is_Leap (2008) AS [2008]

Example 2:

After creating the function copy this into your Query Pane

Print dbo.Is_Leap (2009)

CREATE FUNCTION [dbo].[Is_Leap] 
----------------------------------------------------------------------------------
--        This function figures out if the @year is a leap year. In which case
--        the fucntion will return 1.
--
--        FYI: 
--            Leap years are those years that are evenly divisible by 4, except for 
--            centennial years (those ending in -00), which receive the extra 
--            day only if they are evenly divisible by 400
--    Output:
--        tinyint
--    
--    Created By : Maysarah (ana_maysarah@hotmail.com)
--    Created On : 2004-May-30
----------------------------------------------------------------------------------
    (
    @Year smallint
    )
RETURNS bit
AS
BEGIN

--    1. ______________Not a multiple of 4 -------------------------> NO
    IF @Year % 4 <> 0 RETURN 0

--    2. ______________A multiple of 4 but NOT Centennial ----------> YES
    IF @Year % 100 <> 0 RETURN 1

--    3. ______________A Centennial and a multiple of 400 ----------> YES
    IF @Year % 400 = 0 RETURN 1

--    4. ______________A Centennial but NOT a multiple of 400 ------> NO
    RETURN 0
    


END

Rate

1.33 (3)

Share

Share

Rate

1.33 (3)