SQL 判断年份是否为润年

DROP FUNCTION [dbo].[fn_IsLeapYear]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_IsLeapYear]    Script Date: 2016/2/25 13:22:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*

	select dbo.[fn_IsLeapYear] (year(getdate()))
	select dbo.[fn_IsLeapYear] (2008)  
	select dbo.[fn_IsLeapYear] (2001)  

*/

create function [dbo].[fn_IsLeapYear] 
(     
	@year int 
) 
returns varchar(14)
as begin    

	 declare @returnvalue int
		----varchar(14)     

	 declare @setvalue int     
 
	 set @setvalue=datepart(mm,dateadd(dd,1,cast((cast(@year as varchar(4))+ '0228') as datetime)))     
 
	 if(@setvalue=2)        
		set @returnvalue=1
			---@returnvalue='闰年'     
	 else        
		set @returnvalue=0
			---@returnvalue='非闰年'     
 
	 ----return (cast (@year as varchar(8))+'年:'+@returnvalue) 
	
	return @returnvalue

 end
GO


V2:

/****** Object:  UserDefinedFunction [dbo].[fn_IsLeapYear]    Script Date: 2016/2/25 13:22:50 ******/
DROP FUNCTION [dbo].[fn_IsLeapYearV2]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_IsLeapYear]    Script Date: 2016/2/25 13:22:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*

	select dbo.[fn_IsLeapYearV2] (year(getdate()))
	select dbo.[fn_IsLeapYearV2] (2008)  
	select dbo.[fn_IsLeapYearV2] (2001)  

*/

create function [dbo].[fn_IsLeapYearV2] 
(     
	@year int 
) 
returns varchar(14)
as begin    

	 declare @returnvalue int
	 
	 set @returnvalue=CASE WHEN (@year % 4 = 0 AND @year % 100 <> 0)
or (@year % 400 = 0) THEN 1 ELSE 0 END
  
	return @returnvalue

 end
GO




你可能感兴趣的:(SQL 判断年份是否为润年)