SQL Server中判断闰年的函数

来源:互联网 发布:淘宝卖家账号交易 编辑:程序博客网 时间:2024/04/29 11:41

--1.

 

CREATE FUNCTION IsLeapYear(@Year CHAR(4))

RETURNS BIT

AS

BEGIN

 DECLARE @FirstDayInFeb DATETIME

 DECLARE @LastDayInFeb DATETIME

 DECLARE @ReturnValue BIT

 

 SET @FirstDayInFeb = CONVERT(DATETIME, @Year+'-02-01')

 SET @LastDayInFeb = DATEADD(M, 1, @FirstDayInFeb)

 SET @LastDayInFeb = DATEADD(D, -1, @LastDayInFeb)

 

 IF DATEPART(D, @LastDayInFeb) = 29

   SET @ReturnValue = 1

 ELSE

   SET @ReturnValue = 0

 

 RETURN @ReturnValue

END

 

GO

select dbo.IsLeapYear(1900)

 

--2.

CREATE FUNCTION IsLeapYear(@Year CHAR(4))

RETURNS BIT

AS

BEGIN

    DECLARE @ReturnValue BIT

    DECLARE @iYear INT

    SET @iYear=CONVERT(INT,@Year)

 

    IF (@iYear % 4 = 0 and @iYear % 100 != 0) or (@iYear % 400 = 0)

        SET @ReturnValue = 1

    ELSE

        SET @ReturnValue = 0

    RETURN @ReturnValue

END

 

GO

select dbo.IsLeapYear(2000)

 

 

 

原创粉丝点击