Home » SQL & PL/SQL » SQL & PL/SQL » Number problem
Number problem [message #3457] Sat, 28 September 2002 04:44 Go to next message
randycgaz
Messages: 6
Registered: September 2002
Junior Member
I'm trying to figure out how to check to see if the year is evenly divisible by 4. I'm not sure how to see if 'v_result' is a whole number.

ACCEPT p_leapyear PROMPT 'Please enter a year: '
DECLARE
v_leapyear NUMBER(4) := &p_leapyear;
v_result NUMBER(4);
BEGIN
v_result := v_leapyear / 4;
IF v_result ????? THEN
DBMS_OUTPUT.PUT_LINE (v_leapyear||' is a leap year.');
ELSE
DBMS_OUTPUT.PUT_LINE (v_leapyear||' is not a leap year.');
END IF;
END;

I tried converting it to_char(v_result,'9999'), to_char(v_result,'9999.99') and using date format fmYYYY, but so far no luck.
Re: Number problem--solved [message #3458 is a reply to message #3457] Sat, 28 September 2002 05:23 Go to previous messageGo to next message
randycgaz
Messages: 6
Registered: September 2002
Junior Member
I got it---

v_result := MOD(v_leapyear, 4);
IF v_result > 0 THEN
Re: Number problem [message #3460 is a reply to message #3457] Sat, 28 September 2002 18:52 Go to previous message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Remember that dividing by 4 is only one of the leap year checks. A more robust solution would use the built-in Oracle date datatype to tell if a year was a leap year or not.

declare
  v_year  integer;
  v_date  date;
begin
  v_year := 2002;
  v_date := to_date('02/29/' || v_year, 'mm/dd/yyyy');
  dbms_output.put_line(v_year || ' is a leap year');
exception
  when others then
    dbms_output.put_line(v_year || ' is NOT a leap year');
end;
Previous Topic: Please help, using the same field under different conditions
Next Topic: How to find the nth maximum
Goto Forum:
  


Current Time: Sun Apr 28 21:55:03 CDT 2024