Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SQL Syntax help

Author  Topic 

webmike33
Starting Member

1 Post

Posted - 2003-01-27 : 18:36:05
I am trying to write a function that will accept a date and tell me if a year is a leap year or not.
What I have so far is not working.
Any help would be greatly appreciated.

CREATE FUNCTION LeapYear

(@inputDate datetime)

RETURNS int
AS Begin
Return year((inputDate/ 4 = 0 && inputDate / 100 <> 0) || inputDate / 400 = 0)

End


jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-01-27 : 20:31:17
Here's why it isn't working:

First off, I think you want to use the % operator, not the division operator.

Then, you need to refer to @inputdate at all times with the @ in front of it.

ALso, you cannot put as much as you have within the Year() function -- you need to enclose each @inputdate within its own Year() function.

Also, I don't think SQL has the && and || operators -- use AND and OR.

Finally, SQL can't return a boolean, you need to use a CASE statement and tell it exactly which values to return based on which expressions.

Try:

CREATE FUNCTION LeapYear (@inputDate datetime)
RETURNS int
AS
BEGIN
RETURN (CASE WHEN ((Year(@InputDate) % 4 = 0 ) and (Year(@InputDate) % 100 <> 0)) OR (Year(@inputDate) % 400 = 0) THEN 1 ELSE 0 END)
END

- Jeff
Go to Top of Page
   

- Advertisement -