To get the remainder in Oracle, you can use the mod()
or remainder()
functions. Below are the syntax and examples for the same:
Mod Function Syntax
mod(n2, n1)
MOD gives you the remainder when dividing n2
by n1
. If n1 is zero, it gives back the value for n2
.
Get Remainder in Oracle Using Mod Function
The following SQL query gets the remainder of 5 divided by 2:
select mod(5,2) from dual;
Output:
1
Remainder Function Syntax
remainder(n2, n1)
REMAINDER gives the remainder value after dividing n2
by n1
.
Using Remainder Function
The following SQL query returns the remainder of 9 divided by 3:
select remainder(9,3) from dual;
Output:
0
Using Mod Function in PL/SQL Program
The below PL/SQL program checks if the number is odd or even:
Declare
n_num number := 7;
Begin
if mod(n_num, 2) = 0 then
dbms_output.put_line('Number is even.');
else
dbms_output.put_line('Number is odd.');
end if;
End;
Output:
Number is odd.