# leap years and t-sql

• I need to determine whether a year is a leap year, as I have a date sensitive activity that needs to run within three days of each month end. Short of writing a user defined fucntion, is there something in sql that will allow me to determine if a year is a leap year?

Any help woudl be most appreciated.

Regards,

Morpheus

"See how deep the rabbit holes goes..."

"See how deep the rabbit hole goes..."

• Try this......

select isdate('29 Feb 2003') = 0 (False)

select isdate('29 Feb 2004') = 1 (True)

• Thanks. In hindsight it is rather obvious I guess.

morphuez

quote:

Try this......

select isdate('29 Feb 2003') = 0 (False)

select isdate('29 Feb 2004') = 1 (True)

"See how deep the rabbit holes goes..."

"See how deep the rabbit hole goes..."

• You may not need to know if the year is leap or not:

You can always do the Following

1. Find next month number

2. create the First Day of that next month

3. Substract 3 days of the above date

and you don't need to know anything about leap-year

* Noel

• Yup, you're right. I don't know where my head was when I did the post. I've actually used dateadd() and datediff() and not had to do anything about leap year at all!! You'd think I would have know better...

😉

quote:

You may not need to know if the year is leap or not:

You can always do the Following

1. Find next month number

2. create the First Day of that next month

3. Substract 3 days of the above date

and you don't need to know anything about leap-year

"See how deep the rabbit holes goes..."

"See how deep the rabbit hole goes..."

• A simple solution is the basic test for leap year:

Declare @nYear int

Declare @bLeapYear int

SET @nYear = Year( getdate())

SET @bLeapYear = 0

if ((( @nYear % 4) = 0) AND ((( @nYear % 100) <> 0) OR ((@nYear % 400) = 0)))

SET @bLeapYear = 1

MTC

Thierry

Viewing 6 posts - 1 through 5 (of 5 total)