判断闰年的程序代码SQL(判断闰年的java)
sql编程题,判断某一年(y)是否为闰年?
用函数
create function [dbo].is_runnian1(@year int) returns char(10)
as
begin
return
CASE WHEN (@year % 4 = 0 AND @year % 100 0)
or (@year % 400 = 0)
then '是闰年'
else '不是闰年'
end
end
执行
select dbo.is_runnian1(2000)
括号里面是年份
用SQL语句编写程序:在计算机系统中获取当前的年份,并判断其是否为闰年
--sqlserver
declare @Year int
set @Year = YEAR(GETDATE())
if @Year % 4 = 0
select '闰年'
else
select '非闰年'
用SQL输入1900-2013之间的闰年。(能400整除或能被4整除并且不能被100整除)
这个判断比你说的判断准确
create table table1(edate datetime,year varchar(10))
DECLARE @begindate datetime
DECLARE @enddate datetime
set @begindate = '1900-01-01 00:00:00.000'
set @enddate = '2013-01-01 00:00:00.000'
while @begindate = @enddate
begin
insert into table1
Select @begindate, case day(dateadd(mm, 2, dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,@begindate), 0)))) when 28 then '平年' else '闰年' end
set @begindate = dateadd(yyyy,1,@begindate)
end
select * from table1
如果按能400整除或能被4整除并且不能被100整除判断,更简单,语句:
create table table1(edate int,year varchar(10))
DECLARE @begindate int
DECLARE @enddate int
set @begindate = 1900
set @enddate = 2013
while @begindate = @enddate
begin
if (((@begindate%400=0) or (@begindate%4=0)) and (@begindate%1000))
insert table1 values (@begindate,'闰年')
else
insert table1 values (@begindate,'平年')
set @begindate = @begindate +1
end
select * from table1
sql判断输入的年份是否是闰年
用函数
create function [dbo].is_runnian1(@year int) returns char(10)
as
begin
return
CASE WHEN (@year % 4 = 0 AND @year % 100 0)
or (@year % 400 = 0)
then '是闰年'
else '不是闰年'
end
end
执行
select dbo.is_runnian1(2000)
括号里面是年份