Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help using Modulo Arithmetic Operator

Status
Not open for further replies.

aspengal

Technical User
Dec 27, 2004
41
US
I wanna calculate the Leap year and I have this

declare @which_year numeric(4,0)

if(@which_year%4) = 0
--is a lear year
else
--not a leap year

this wont work as it gives datatype error....help pls....

 
A year will always be an int value, you need this for the modulo, so change your declaration to be this type and it will be ok.
Code:
declare @which_year int --numeric(4,0)

if(@which_year%4) = 0
--is a lear year
else
--not a leap year

"I'm living so far beyond my income that we may almost be said to be living apart
 
Just because a year divides exactly by 4 does not make it a leap year.

If a year is a century year it must divide by 400 to be a leap year. (1900 was not a leap year but 2000 was)

 
Dirty way:
Code:
select isdate (convert(varchar(4), @year) + '0229')

------
heisenbug: A bug that disappears or alters its behavior when one attempts to probe or isolate it
schroedinbug: A bug that doesn't appear until someone reads source code and realizes it never should have worked, at which point the program promptly stops working for everybody until fixed.

[ba
 
Older - lazier [pipe]. A month ago I would probably suggest this:
Code:
if @year % 4 = 0 and (@year % 100 <> 0 or @year % 400 = 0)
-- is a leap year
else
--not a leap year
... and year ago:
Code:
select 1 - (sign(@year % 4) | (~sign(@year % 100) & sign(@year % 400)))
I don't even wanna think about five years ago... brrrr.

------
heisenbug: A bug that disappears or alters its behavior when one attempts to probe or isolate it
schroedinbug: A bug that doesn't appear until someone reads source code and realizes it never should have worked, at which point the program promptly stops working for everybody until fixed.

[ba
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top