//閏年最直接的判斷是:能被4整除且不能被100整除,或者能被400整除的年份
create or replace procedure pro_leap_year(year_in in number) as
v_mod1 number(4) :=mod(year_in,4);
v_mod2 number(4) :=mod(year_in,100);
v_mod3 number(4) :=mod(year_in,400);
begin
if ((v_mod1=0 and v_mod2<>0) or v_mod3=0) then
dbms_output.put_line(year_in||' is a leap year');
else dbms_output.put_line(year_in||' is not a leap year');
end if;
exception
when others then
dbms_output.put_line(sqlerrm);
end pro_leap_year;
/ sql> exec pro_leap_year(2010);
2010 is not a leap year
pl/sql procedure successfully completed
//
sql> exec pro_leap_year(2000);
2000 is a leap year
pl/sql procedure successfully completed
//
//關於閏年平年,下面有乙個比較全面的解釋:
//在公曆(格里曆)紀年中,有閏日的年份叫閏年,一般年份365天,閏年為366天。
//由於地球繞太陽執行週期為365天5小時48分46秒(合365.24219天)即一回歸年,公曆把一年定為365天。
//所餘下的時間約為四年累計一天,加在二月裡,所以平常年份每年365天,二月為28天,閏年為366天,二月為29天。
//因此,每400年中有97個閏年,閏年在2月末增加一天,閏年366天。
//閏年的計算方法:公元紀年的年數可以被四整除,即為閏年;
//被100整除而不能被400整除為平年;被100整除也可被400整除的為閏年。
//如2023年是閏年,而2023年不是。
// //我們所關心的是怎麼樣計算指定的乙個年份是否是閏年:
//從上面的描述中,我們知道:
//如果一年中2月份有29天,那麼這一年就是閏年,否則是平年
sql> drop procedure pro_leap_year;
procedure dropped
//
create or replace procedure pro_leap_year(year_in in number) as
v_year_in varchar2(10) :=to_char(year_in)||'0229';
v_date date;
begin
//這裡將拼接的字串轉換為日期,並賦值給乙個日期型別的變數,
//其實就是為了和日期型別進行比較,隱式的比較,比較你輸入的年份中2月是否含有29日這一天
v_date :=to_date(v_year_in,'yyyy-mm-dd');
dbms_output.put_line(year_in||' is a leap year');
exception
when others then
dbms_output.put_line(year_in||' is not a leap year');
end pro_leap_year;
sql> exec pro_leap_year(2010);
2010 is not a leap year
pl/sql procedure successfully completed
//
sql> exec pro_leap_year(2000);
2000 is a leap year
pl/sql procedure successfully completed
//
sql> exec pro_leap_year(2005);
2005 is not a leap year
pl/sql procedure successfully completed
//
sql> exec pro_leap_year(2100);
2100 is not a leap year
pl/sql procedure successfully completed
//
//我們也可以用一條select語句來判斷:
sql> select case
2 when to_char(last_day(to_date(&year||'02','yyyymm')),'dd')='29'
3 then 'the year you input is a leap year'
4 else
'the year you input is not a leap year'
5 end is_leap_year
6 from dual;
enter value for year: 2050
old 2: when to_char(last_day(to_date(&year||'02','yyyymm')),'dd')='29'
new 2: when to_char(last_day(to_date(2050||'02','yyyymm')),'dd')='29'
is_leap_year
the year you input is not a leap year
//
sql> /
enter value for year: 2000
old 2: when to_char(last_day(to_date(&year||'02','yyyymm')),'dd')='29'
new 2: when to_char(last_day(to_date(2000||'02','yyyymm')),'dd')='29'
is_leap_year
the year you input is a leap year
//
sql> /
enter value for year: 2012
old 2: when to_char(last_day(to_date(&year||'02','yyyymm')),'dd')='29'
new 2: when to_char(last_day(to_date(2012||'02','yyyymm')),'dd')='29'
is_leap_year
the year you input is a leap year
//
sql> /
enter value for year: 1998
old 2: when to_char(last_day(to_date(&year||'02','yyyymm')),'dd')='29'
new 2: when to_char(last_day(to_date(1998||'02','yyyymm')),'dd')='29'
is_leap_year
the year you input is not a leap year //
原帖:
判斷閏年(七)
閏年的乙個基本規則是 四年一閏,百年不閏,四百年再閏。通俗一點就是 能夠被4整除,但同時不能被100整除卻能被400整除的年份就叫閏年。這裡面最關鍵的一句 就是 if year 4 0 year 100 0 year 400 0 示例 include include using namespace ...
2883 閏年判斷
time limit 1 sec memory limit 128 mb submit 124 solved 68 submit status web board 定義乙個函式leapyear,函式宣告如下 int leapyear int n 函式宣告,如果引數n是閏年返回1,不是閏年返回0 在下...
pl sql 判斷閏年
這是乙個老調重彈的例子,也許以前寫過這樣的筆記,這裡再貼出來,是再次感嘆於其解決方法的美妙行。判斷乙個年份是閏年,常見的想法是判斷其是否可被4整除,如果是100的倍數,要求其能被400整除。下面是摘自dba village的例子 create or replace function is leap ...