Sub topic 6
Choosing scalar data types
This chapter assumes you can write basic SQL and know the Oracle column types NUMBER, VARCHAR2, and DATE. It also assumes you can run an anonymous block in SQL*Plus, SQL Developer, or SQLcl. Every variable in a PL/SQL block needs a data type. This chapter covers how to choose one.
What "scalar" means
A scalar type holds a single value with no internal parts. That sets it apart from composite types such as records and collections, which hold several values. Oracle groups scalar types into families:
| Family | Common types | Typical use |
|---|---|---|
| Numeric | NUMBER, PLS_INTEGER, BINARY_INTEGER, SIMPLE_INTEGER, BINARY_FLOAT, BINARY_DOUBLE | Money, counters, measurements |
| Character | VARCHAR2, CHAR, NVARCHAR2, LONG | Names, codes, free text |
| Date and time | DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, INTERVAL | Dates, audit times, durations |
| Boolean | BOOLEAN | Flags in program logic |
| Large object | CLOB, BLOB | Documents, binary data |
PL/SQL accepts every SQL column type. It also adds a few of its own, such as PLS_INTEGER and BOOLEAN. The next sections cover the types you will use most.
Numeric types
NUMBER
NUMBER is the general-purpose decimal type. It matches the NUMBER column type in SQL. You can give it a precision (total significant digits) and a scale (digits after the decimal point). NUMBER(10,2) holds values up to 99,999,999.99. When a value has more decimal places than the scale allows, PL/SQL rounds it on assignment. It does not raise an error.
NUMBER stores decimal fractions exactly. That makes it the right choice for money and other business values.
PLS_INTEGER and its relatives
PLSINTEGER is a 32-bit signed integer, with a range of -2,147,483,648 to 2,147,483,647. It uses hardware arithmetic, so it is usually faster than NUMBER for loop counters and array indexes. BINARYINTEGER is the same type in current Oracle releases.
If an operation leaves the range, Oracle raises ORA-01426 (numeric overflow). This block shows it:
SET SERVEROUTPUT ON
DECLARE
v_count PLS_INTEGER := 2147483647;
BEGIN
v_count := v_count + 1;
DBMS_OUTPUT.PUT_LINE(v_count);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Caught: ' || SQLERRM);
END;
/The output is Caught: ORA-01426: numeric overflow.
SIMPLE_INTEGER has the same range but is always NOT NULL. It wraps around on overflow instead of raising an error. Use it only when you want that wrap-around behavior, such as in hash calculations.
PL/SQL also predefines subtypes of these types. A subtype is a restricted version of a base type. NATURAL allows 0 and above, and POSITIVE allows 1 and above. Assigning -5 to a POSITIVE variable raises VALUE_ERROR.
BINARY_FLOAT and BINARY_DOUBLE
These are binary floating-point types. They are fast, but they cannot represent most decimal fractions exactly. This block compares them with NUMBER:
SET SERVEROUTPUT ON
DECLARE
n_sum NUMBER := 0.1 + 0.2;
d_sum BINARY_DOUBLE := 0.1d + 0.2d;
BEGIN
IF n_sum = 0.3 THEN
DBMS_OUTPUT.PUT_LINE('NUMBER: 0.1 + 0.2 equals 0.3');
ELSE
DBMS_OUTPUT.PUT_LINE('NUMBER: not equal');
END IF;
IF d_sum = 0.3d THEN
DBMS_OUTPUT.PUT_LINE('BINARY_DOUBLE: 0.1 + 0.2 equals 0.3');
ELSE
DBMS_OUTPUT.PUT_LINE('BINARY_DOUBLE: not equal');
END IF;
END;
/The NUMBER comparison is true. The BINARYDOUBLE comparison is false, because the binary sum is slightly above 0.3. The d suffix marks a literal as BINARYDOUBLE.
Choose BINARY_DOUBLE for scientific or statistical work, where speed matters more than exact decimals. Do not use it for money.
Character types
VARCHAR2
VARCHAR2 holds variable-length text. You must declare a maximum length, as in VARCHAR2(50). Leaving it out is a compile error. The length is in bytes by default. You can write VARCHAR2(50 CHAR) to count characters instead. That matters with multibyte character sets such as AL32UTF8.
A PL/SQL VARCHAR2 variable can be up to 32,767 bytes. A SQL column is limited to 4,000 bytes, or 32,767 when extended data types are enabled. This means a large variable can compile and run, yet fail when you insert its value into a column.
Assigning a value longer than the declared length raises VALUE_ERROR (ORA-06502):
SET SERVEROUTPUT ON
DECLARE
v_code VARCHAR2(5);
BEGIN
v_code := 'ABCDEFGH';
DBMS_OUTPUT.PUT_LINE(v_code);
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Value too long: ' || SQLERRM);
END;
/CHAR
CHAR is a fixed-length type. Oracle pads shorter values with trailing spaces up to the declared length. Padding makes comparisons surprising. When both operands are CHAR, Oracle ignores the padding. When one side is VARCHAR2, it compares the exact characters:
SET SERVEROUTPUT ON
DECLARE
c_val CHAR(5) := 'ab';
v_val VARCHAR2(5) := 'ab';
BEGIN
DBMS_OUTPUT.PUT_LINE('CHAR length: ' || LENGTH(c_val));
IF c_val = v_val THEN
DBMS_OUTPUT.PUT_LINE('Equal');
ELSE
DBMS_OUTPUT.PUT_LINE('Not equal');
END IF;
END;
/The block prints CHAR length: 5 and then Not equal. Prefer VARCHAR2 unless a value really has a fixed width.
Date and time types
DATE stores the century, year, month, day, hour, minute, and second. It has no fractional seconds and no time zone. Subtracting two dates gives the number of days, which can be fractional. Adding a number to a date adds that many days.
SET SERVEROUTPUT ON
DECLARE
v_start DATE := TO_DATE('2026-11-28', 'YYYY-MM-DD');
v_due DATE;
BEGIN
v_due := v_start + 30;
DBMS_OUTPUT.PUT_LINE('Due: ' || TO_CHAR(v_due, 'YYYY-MM-DD'));
DBMS_OUTPUT.PUT_LINE('Days: ' || (v_due - v_start));
END;
/The output is Due: 2026-12-28 and Days: 30. Always pass an explicit format mask to TODATE and TOCHAR. Without one, Oracle uses the session's NLSDATEFORMAT setting, so the same code can behave differently for another user.
Use TIMESTAMP when you need fractional seconds. Use TIMESTAMP WITH TIME ZONE when the time zone is part of the data. Subtracting timestamps returns an INTERVAL, not a number.
BOOLEAN
BOOLEAN holds TRUE, FALSE, or NULL. It is a standard PL/SQL type, so you can use it freely for flags in program logic. Two limits apply:
- DBMSOUTPUT.PUTLINE does not accept a BOOLEAN argument. Test the value with IF or CASE and print text instead.
- Older Oracle releases have no BOOLEAN column type in SQL. Oracle Database 23ai adds one. On earlier releases, store flags as
NUMBER(1)orCHAR(1).
SET SERVEROUTPUT ON
DECLARE
v_found BOOLEAN;
BEGIN
IF v_found THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSIF NOT v_found THEN
DBMS_OUTPUT.PUT_LINE('FALSE');
ELSE
DBMS_OUTPUT.PUT_LINE('NULL: the variable was never set');
END IF;
END;
/An uninitialized BOOLEAN is NULL. Neither v_found nor NOT v_found is true when the value is NULL, so the code reaches the ELSE branch.
A decision guide
The diagram shows a rough order of questions to ask about each variable.
Follow this rule for any variable that holds data read from a table: do not choose a type by hand. Anchor it to the column with %TYPE, which a later chapter covers. Then the declaration stays correct when the column changes.
Implicit conversion and common errors
PL/SQL converts between types automatically when it can. For example, it turns the string '42' into a number in v_total := '42' + 1. Implicit conversion is risky. A string that is not a valid number raises VALUEERROR at run time. A date-to-string conversion depends on session settings. Prefer TONUMBER, TOCHAR, and TODATE with explicit formats.
| Symptom | Likely cause | Fix |
|---|---|---|
| ORA-06502, character string buffer too small | Value longer than the VARCHAR2 length | Enlarge the variable, or use `%TYPE` |
| ORA-06502, number precision too large | Value has more digits than the declared precision | Widen the precision |
| ORA-01426, numeric overflow | PLS_INTEGER arithmetic left its range | Use NUMBER for the calculation |
| ORA-01722, invalid number | Implicit conversion of non-numeric text | Validate the text first, then use TO_NUMBER |
| Comparison unexpectedly false | CHAR padding, or NULL | Use VARCHAR2, and handle NULL explicitly |
Summary of choices
- Use NUMBER for exact decimal values, and give it a precision and scale when the business rules call for them.
- Use PLS_INTEGER for small whole-number counters and loop indexes.
- Use BINARY_DOUBLE only when approximate floating-point results are acceptable.
- Use VARCHAR2 for text, with a length large enough for real data.
- Use DATE for calendar values, and TIMESTAMP when you need fractional seconds or time zones.
- Use BOOLEAN for logic flags, and print them through IF or CASE.
Exercise
A block declares v_qty PLS_INTEGER := 2000000000; and then runs v_qty := v_qty * 2;. What happens, and which type change avoids it?
Show answer
The result, 4,000,000,000, is above the PLSINTEGER maximum of 2,147,483,647, so Oracle raises ORA-01426 (numeric overflow). Declaring `vqty` as NUMBER avoids the error, because NUMBER holds far larger values.
Exercise
A block declares v_flag CHAR(3) := 'ok'; and compares it with IF v_flag = v_text THEN, where v_text is VARCHAR2(3) := 'ok'. Does the condition succeed, and why?
Show answer
It fails. The CHAR(3) variable holds 'ok ' with a trailing space. Because one operand is VARCHAR2, Oracle compares the exact characters, and 'ok ' differs from 'ok'. Declaring both variables as VARCHAR2 fixes it.
Exercise
Why does DBMS_OUTPUT.PUT_LINE(v_found); fail to compile when v_found is BOOLEAN, and how do you print its state?
Show answer
PUT_LINE accepts character-compatible arguments, not BOOLEAN. Test the variable with IF, ELSIF, and ELSE (or CASE) and print a text message for TRUE, FALSE, and NULL.