An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulation. PL/SQL language is rich in built-in operators
1. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
2. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Operators in PL/SQL
Arithmetic Operators:
A number:=5; B number:=2;
Operator Usage Result
+ A+B 7
- A-B 3
* A*B 10
/ A/B 2.5
** (Raise to the power) A**B 25
Relational Operators
A number:=5; B number:=2;
Operator Usage Result
= A=B false
!= or <> or ~= A!=B or A<>B or A~=B true
> A>B true
>= A>=B true
< A<B false
<= A<=B false
Note: You cannot use Relational operators in dbms_output.put_line() For example:
DECLARE
vA number:=5;
vB number:=2;
BEGIN
dbms_output.put_line(vA>=vB); - - Error
dbms_output.put_line(‘Result: ’||vA>=vB); - - Error
dbms_output.put_line(‘Result: ’||to_char(vA>=vB)); - - Error
END;
/
You can use relational operators in conditional statements like IF which we will cover in next part.
Logical Operators- Logical operators work on Boolean operands and produces Boolean results.
A Boolean :=true; B Boolean :=false;
3. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Operator Usage Result
AND A AND B false
OR A OR B true
NOT NOT A false
Comparison Operators: Comparison operators are used for comparing one expression to
another. The result is always either TRUE, FALSE OR NULL. So we cannot use in
dbms_output.put_line()
vA varchar2(50) :=’HELLO’;
LIKE
Usage Description Result
A LIKE ‘H%’ matches all string that starts with
H, i.e. H,HH,HHH,….HELLO,HELLO1
etc
true
A LIKE ‘K%’ matches all string that starts with
K
false
A LIKE ‘_ELLO’ matches all string having ‘ELLO’
excluding first character, i.e.
HELLO,KELLO,MELLO,DELLO,JELLO
etc.
true
% matches multiple characters while _ matches single character
BETWEEN
vX number :=10;
Usage Description Result
vX BETWEEN 10 AND 20 checks whether vX lies
between 10 to 20
true
vX BETWEEN 11 AND 20 checks whether vX lies
between 11 to 20
false
vX BETWEEN 10 AND 5 checks whether vX lies
between 10 to 5
false
IN
Usage Description Result
vX IN (10,20,30,50) checks whether vX is: 10 or 20
or 30 or 50
true
vX IN (40,50,30,20) checks whether vX 40 or 50 or
30 or 20
false