Sub topic 4
Literals, arithmetic and string concatenation
This chapter covers how to write fixed values in a query, how to calculate with them, and how to join text. It assumes you can run a query in SQL*Plus, SQL Developer or SQL Worksheet, and that you know what a column alias is from the previous chapters. Most examples use dual, so they run without any application tables. The employees examples use the HR sample schema.
Literals: values written directly in the query
A literal is a value typed into the statement itself instead of read from a column. Oracle gives each kind of literal a data type, and that type decides how the value behaves in expressions.
| Kind | Example | Data type |
|---|---|---|
| Number | `42`, `3.14`, `-7`, `1.5E3` | NUMBER |
| Text | `'Oracle'` | CHAR (a character literal, compared with blank-padded semantics) |
| Date | `DATE '2026-11-28'` | DATE |
| Timestamp | `TIMESTAMP '2026-11-28 07:42:16'` | TIMESTAMP |
| Interval | `INTERVAL '3' DAY` | INTERVAL DAY TO SECOND |
SELECT 42 AS int_lit,
3.14 AS dec_lit,
1.5E3 AS sci_lit,
'text' AS str_lit,
TO_CHAR(DATE '2026-11-28', 'DD-MON-YYYY') AS date_lit
FROM dual;The date literal always uses the YYYY-MM-DD form, whatever the session's NLS_DATE_FORMAT is. Writing '28-NOV-2026' as a plain string and hoping Oracle converts it depends on session settings. A DATE '...' literal does not.
Quoting text
Text literals use single quotes. Double quotes are for identifiers such as "My Column", never for values. To put a single quote inside a text literal, double it, or use the alternative quoting operator q'[...]'.
SELECT 'It''s Oracle' AS doubled_quote,
q'[It's Oracle]' AS q_quoted,
q'{She said "hi"}' AS with_double_quotes
FROM dual;All three columns contain the text you would expect. The q' form accepts [ ], { }, ( ), < > or any other character as a delimiter, as long as the closing delimiter is followed by '. It is most useful for text that contains many quotes, such as SQL statements stored as strings.
The empty string is NULL
Oracle treats a zero-length string as NULL. Most other databases do not. That has consequences:
WHERE name = ''never matches any row, because the comparison isname = NULL.LENGTH('')returns NULL, not 0.- You test for "empty" with
IS NULL.
Arithmetic operators
Oracle supports +, -, * and /. It has no % operator and no ** or ^ operator. Use MOD for the remainder and POWER for exponents.
SELECT 7 + 2 AS plus_r,
7 - 2 AS minus_r,
7 * 2 AS times_r,
7 / 2 AS divide_r,
MOD(7, 2) AS remainder_r,
POWER(7, 2) AS power_r
FROM dual;The results are 9, 5, 14, 3.5, 1 and 49. The division result matters: 7 / 2 is 3.5. Oracle's / on NUMBER values is true division, not integer division as in C or Java. For a whole-number quotient, apply TRUNC(7 / 2) or FLOOR(7 / 2). They differ for negative values: TRUNC(-7 / 2) is -3 and FLOOR(-7 / 2) is -4.
Precedence
Multiplication and division bind tighter than addition and subtraction. Parentheses override this.
SELECT 2 + 3 * 4 AS no_parens,
(2 + 3) * 4 AS with_parens,
-2 * 3 + 10 AS unary_minus
FROM dual;The results are 14, 20 and 4. The unary minus is applied first, so the third expression is (-2) * 3 + 10.
Arithmetic in a real query
You can use expressions anywhere a column can appear in the select list. Give each computed column an alias, because otherwise the heading is the raw expression text.
SELECT first_name,
salary,
salary * 12 AS annual_salary,
ROUND(salary * 1.05, 2) AS after_raise
FROM employees
WHERE department_id = 50;The expression is calculated for each row. It never changes the stored data. A SELECT only reads.
NULL propagates through arithmetic
If any operand of an arithmetic operator is NULL, the result is NULL.
SELECT 5 + NULL AS plus_null,
NULL * 0 AS times_zero,
5 + NVL(NULL, 0) AS safe_sum
FROM dual;The first two columns are NULL. Even NULL * 0 is NULL, not 0. The third column returns 5 because NVL replaces the NULL first. In the HR schema, salary + salary * commission_pct gives NULL for every employee without a commission. The usual fix is salary + salary * NVL(commission_pct, 0).
Division by zero
Dividing by zero raises an error instead of returning NULL or infinity.
SELECT 1 / 0 AS boom FROM dual;This fails with ORA-01476: divisor is equal to zero. When the divisor comes from data, guard it with NULLIF, which returns NULL when its two arguments are equal. Dividing by NULL gives NULL, so the query completes.
SELECT 100 / NULLIF(0, 0) AS safe_division FROM dual;Date arithmetic
Adding a number to a DATE adds that many days. Subtracting two dates gives the difference in days, which may be fractional because a DATE carries a time of day.
SELECT TO_CHAR(DATE '2026-11-28' + 7, 'YYYY-MM-DD') AS next_week,
DATE '2026-12-25' - DATE '2026-11-28' AS days_left,
TO_CHAR(DATE '2026-11-28' + 12/24, 'YYYY-MM-DD HH24:MI') AS noon
FROM dual;The results are 2026-12-05, 27 and 2026-11-28 12:00. One day is the unit, so 12/24 is half a day. Adding two dates is not allowed. For month arithmetic use ADD_MONTHS, not + 30.
String concatenation
The concatenation operator is two vertical bars, ||. It joins character values.
SELECT employee_id,
first_name || ' ' || last_name AS full_name,
last_name || ', ' || first_name AS sort_name
FROM employees
WHERE department_id = 50
ORDER BY last_name;The CONCAT function does the same job but accepts exactly two arguments, so joining three pieces needs nesting: CONCAT(CONCAT(first_name, ' '), last_name). For that reason most Oracle code uses ||.
NULL in concatenation
Concatenation treats NULL as an empty string. This is the one common place where NULL does not propagate.
SELECT 'A' || NULL || 'B' AS joined,
NULL || NULL AS both_null
FROM dual;The first column is AB. The second is NULL, because the result is an empty string and Oracle stores empty strings as NULL. A missing middle name therefore leaves a doubled space in first || ' ' || middle || ' ' || last. Handle each optional part explicitly, for example first_name || NVL2(middle_name, ' ' || middle_name, '') || ' ' || last_name.
Implicit conversion with ||
When one operand is not text, Oracle converts it to text for you.
SELECT 'Order #' || 1001 AS with_number,
'Today is ' || TO_CHAR(DATE '2026-11-28', 'DD Month YYYY') AS with_date
FROM dual;Numbers convert without surprise in the first column. For dates, always call TO_CHAR with a format mask. Without it, the text depends on the session's NLS_DATE_FORMAT, and the same query prints different output on different clients.
Mixing arithmetic and concatenation
In Oracle, binary +, binary - and || have the same precedence. They are evaluated from left to right. This leads to a classic bug:
SELECT 'Total: ' || 2 + 3 AS broken FROM dual;Oracle reads this as ('Total: ' || 2) + 3. It tries to turn the text Total: 2 into a number and fails with ORA-01722: invalid number.
Parentheses fix it:
SELECT 'Total: ' || (2 + 3) AS total_text FROM dual;This returns Total: 5. As a habit, put every arithmetic sub-expression inside parentheses when it appears next to ||.
Implicit conversion in arithmetic
The reverse direction also works at times. If an operand of an arithmetic operator is text, Oracle tries to convert it to a number.
SELECT '10' + 5 AS works FROM dual;The result is 15. With 'abc' + 5 the conversion fails with ORA-01722. This is risky in real queries. A column that holds text numbers can work for years and then fail the day someone inserts a bad value. Convert explicitly instead, with TO_NUMBER('10') + 5, and decide what to do about bad input. In Oracle Database 12.2 and later, TO_NUMBER('abc' DEFAULT 0 ON CONVERSION ERROR) returns a fallback instead of raising an error.
Common errors
| Symptom | Likely cause | Fix | Column | Column |
|---|---|---|---|---|
| `ORA-01722: invalid number` | Text used as a number, often `'text' | 2 + 3` | Add parentheses, or convert with `TO_NUMBER` | |
| `ORA-01476: divisor is equal to zero` | Divisor is 0 | Use `NULLIF(divisor, 0)` | ||
| `ORA-00923: FROM keyword not found where expected` | A missing comma, or an alias with a space that is not quoted | Check the select list punctuation | ||
| `ORA-00904: invalid identifier` | Text written with double quotes instead of single quotes | Use `'text'` for values | ||
| Result is NULL where you expected a number | A NULL operand in arithmetic | Wrap it with `NVL(x, 0)` | ||
| Extra spaces in a joined name | NULL parts treated as empty strings | Build optional parts with `NVL2` |
Exercises
Exercise
What does SELECT 'A' || NULL || 'B', 5 + NULL FROM dual return, and why do the two columns behave differently?
Show answer
The first column is AB and the second is NULL. The || operator treats NULL as an empty string, so the joined text is just AB. Arithmetic operators return NULL whenever any operand is NULL, so 5 + NULL is NULL.
Exercise
The query SELECT 'Total: ' || 2 + 3 FROM dual fails with ORA-01722. Explain why, and rewrite it so that it returns Total: 5.
Show answer
Oracle gives + and || the same precedence and evaluates them left to right, so the expression is ('Total: ' || 2) + 3. Oracle then tries to convert the text 'Total: 2' to a number and fails. Adding parentheses fixes it: SELECT 'Total: ' || (2 + 3) FROM dual.
Exercise
Write a query on dual that returns the number of days from DATE '2026-11-28' to DATE '2027-01-01', and a second column that returns the whole-number result of 17 divided by 5.
Show answer
SELECT DATE '2027-01-01' - DATE '2026-11-28' AS daysbetween, TRUNC(17 / 5) AS wholequotient FROM dual; The first column is 34 and the second is 3. Plain 17 / 5 gives 3.4, because / is not integer division in Oracle.