Oracle PL/SQL - BETWEEN Operator for value range

Introduction

The BETWEEN operator tests whether a value is in a specified range.

x BETWEEN a AND b returns the same value as ( x >= a ) AND ( x <= b ).

The following code prints the values of expressions that include the BETWEEN operator.

Demo

SQL>
SQL> CREATE OR REPLACE PROCEDURE print_boolean (
  2    b_name   VARCHAR2,-- from  ww w .  jav  a2  s . c  o m
  3    b_value  BOOLEAN
  4   ) IS
  5  BEGIN
  6    IF b_value IS NULL THEN
  7      DBMS_OUTPUT.PUT_LINE (b_name || ' = NULL');
  8    ELSIF b_value = TRUE THEN
  9      DBMS_OUTPUT.PUT_LINE (b_name || ' = TRUE');
 10    ELSE
 11      DBMS_OUTPUT.PUT_LINE (b_name || ' = FALSE');
 12    END IF;
 13  END;
 14  /

Procedure created.

SQL> BEGIN
  2    print_boolean ('2 BETWEEN 1 AND 3', 2 BETWEEN 1 AND 3);
  3    print_boolean ('2 BETWEEN 2 AND 3', 2 BETWEEN 2 AND 3);
  4    print_boolean ('2 BETWEEN 1 AND 2', 2 BETWEEN 1 AND 2);
  5    print_boolean ('2 BETWEEN 3 AND 4', 2 BETWEEN 3 AND 4);
  6  END;
  7  /
2 BETWEEN 1 AND 3 = TRUE
2 BETWEEN 2 AND 3 = TRUE
2 BETWEEN 1 AND 2 = TRUE
2 BETWEEN 3 AND 4 = FALSE

PL/SQL procedure successfully completed.

SQL>
SQL>