Previous | Table of Contents | Next

Page 71

Negation and Identity

You are familiar with negation, in its simplest form, when you use it to write a negative value such as _242.24. One way to look at it would be to say that the _ preceding the number indicates that the value is negative. Another way to look at this is that you wrote a positive number, in other words 242.24, and applied the negation operator to it, thus yielding a negative value. Take the latter approach, and you will quickly realize that the target of the negation operator could just as well be a variable or an expression.

The identity operator, represented by the plus sign, doesn't do much at all. It's the opposite of the negation operator and simply returns the value of its operand.

Listing 4.2 shows some examples of how you can use these two operators.

INPUT
Listing 4.2. The negation and identity operators in action.

 1: --The negation and identity operators in action.
 2: --Remember to execute: SET SERVEROUTPUT ON
 3: DECLARE
 4:   x   NUMBER;
 5: BEGIN
 6:   DBMS_OUTPUT.PUT_LINE(-242.24);
 7:   --You can also negate a variable.
 8:   x := 5;
 9:   DBMS_OUTPUT.PUT_LINE(-x);
10:   --Negating a negative number yields a positive value.
11:   x := -5;
12:   DBMS_OUTPUT.PUT_LINE(-x);
13:   --The identity operator simply returns the value of its operand.
14:   DBMS_OUTPUT.PUT_LINE(+10);
15:   DBMS_OUTPUT.PUT_LINE(+x);
16: END;
17: /

OUTPUT

-242.24
-5
5
10
-5
PL/SQL procedure successfully completed.

ANALYSIS
In line 6, the negation operator is used to simply write a negative number. In lines 7 through 9, the negation operator is used to negate the value of a variable. In line 12, it is used again, but this time the variable contains a negative number to start with, so the resulting value is positive. The remaining lines show the identity operator returning the operand's value unchanged.

Page 72

Comparison Operators

Comparison operators are used to compare one value or expression to another. You can use them in your code to ask questions such as "Are these two values equal?" and then make decisions based on the result. Table 4.3 lists all the comparison operators supported by
PL/SQL.

Table 4.3. Comparison operators.

Operator Example Usage
= IF A = B THEN The equality operator. This compares two values to see if they are identical.
<> IF A <> B THEN The inequality operator. This compares two values to see if they are not identical.
!= IF A != B THEN Another inequality operator, synonymous with <>.
~= IF A ~= B THEN Another inequality operator, synonymous with <>.
< IF A < B THEN The less than operator. Checks to see if one value is less than another.
> IF A > B THEN The greater than operator. Checks to see if one value is greater than another.
<= IF A <= B THEN The less than or equal to operator. Checks to see if one value is less than or equal to another.
>= IF A >= B THEN The greater than or equal to operator. Checks to see if one value is greater than or equal to another.
LIKE IF A LIKE B THEN The pattern-matching operator. Checks to see if a character string matches a specified pattern.
BETWEEN IF A BETWEEN B AND C THEN Checks to see if a value lies within a specified range of values.
IN IF A IN (B,C,D) THEN Checks to see if a value lies within a specified list of values.
IS NULL IF A IS NULL THEN Checks to see if a value is null.

Page 73

NEW TERM
The first eight operators shown in Table 4.3 are referred to as relational operators. These operators are very common and are present in almost any programming language.

All comparison operators return a boolean result. They either represent a true statement or they do not. This true/false, or boolean, result can be used in a branching statement such as an IF...THEN statement, or it can be assigned to a boolean variable for later reference.

With the exception of the LIKE operator, you can use all comparison operators with any of the scalar datatypes discussed on Day 3, "Writing Declarations and Blocks." The LIKE operator is only valid for character strings.

The Relational Operators: =, <>, !=, ~=, <, >, <=, >=
Like the basic arithmetic operators, the relational operators are commonly used in almost every programming language, and there is probably no need to elaborate much on the explanations in Table 4.3.

The relational operators test values for equality, inequality, and to see if one value is less than or greater than another. You can use these operators to compare two values belonging to any of the scalar datatypes. Table 4.4 gives some sample true and false expressions.

Table 4.4. Relational operator examples.

True Expressions False Expressions
5 = 5 5 = 3
`Jonathan' = `Jonathan' `Jonathan ` = `Jonathan'
5 != 3 5 <> 5
`Jonathan ` ~= `Jonathan' `Jonathan` ~= `Jonathan'
10 < 200 10.1 < 10.05
`Jeff' < `Jenny' `jeff' < `Jeff'
TO_DATE('15-Nov-61' < '15-Nov-97') TO_DATE(`1-Jan-97' < `1-Jan-96')
10.1 <= 10.1 10 <= 20
`A' <= `B' `B' <= `A'
TO_DATE(`1-Jan-97') <= TO_DATE('15-Nov-61') <=
TO_DATE(`1-Jan-97) TO_DATE('15-Nov-60)

You should be aware of some considerations when comparing dates and character strings. Oracle dates contain a time component, and it's important to remember that when comparing two dates for equality. String comparisons are case-sensitive, are dependent on the character set being used, and are affected by the underlying datatype. Comparing two values

Previous | Table of Contents | Next

Используются технологии uCoz