Click to See Complete Forum and Search --> : Leap Year


Tony
10-23-2003, 02:56 PM
Can anyone tell me a quick way to build a UDF in SQL SERVER 2000 that tells
me whether or not an inputed year is a a leap year?

Thanks in advance

David Satz
10-23-2003, 02:56 PM
here's one for you:

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[is_leap_year]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[is_leap_year]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE FUNCTION dbo.is_leap_year
(@year char(4))
RETURNS bit
AS
BEGIN
DECLARE @returnValue bit
IF datepart(day, dateadd( day, -1, cast( @year + '0301' as datetime ) ) ) =
29
SET @returnValue = 1
ELSE
SET @returnValue = 0

RETURN @returnValue
END
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO






SELECT dbo.is_leap_year('1999') AS LY1999
, dbo.is_leap_year('2000') AS LY2000
, dbo.is_leap_year('2001') AS LY2001
, dbo.is_leap_year('2002') AS LY2002
, dbo.is_leap_year('2003') AS LY2003
, dbo.is_leap_year('2004') AS LY2004
, dbo.is_leap_year('2005') AS LY2005


--
HTH,
David Satz
Principal Web Engineer
Hyperion Solutions

"Tony" <tinmantrigger@hotmail.com> wrote in message
news:3f85e167@devx7.web.devx.com...
>
> Can anyone tell me a quick way to build a UDF in SQL SERVER 2000 that
tells
> me whether or not an inputed year is a a leap year?
>
> Thanks in advance
>