Sub topic 6
Using DUAL for SYSDATE, sequences and quick tests
This chapter shows three everyday jobs for SELECT ... FROM dual: reading the current date and time, drawing values from a sequence, and testing expressions without touching an application table. It assumes you can already write a basic SELECT, know that DUAL is a one-row, one-column table, and can run SQL in SQL*Plus, SQL Developer or SQL Worksheet.
Why these jobs need a row source
In most Oracle releases, a SELECT must have a FROM clause. A query returns one result row for each row of its row source. SYSDATE, NEXTVAL and a plain expression such as 2 + 3 do not come from any table, so they need a row source that produces exactly one row. DUAL is that row source.
This has two consequences.
- Querying
DUALreturns one row, so each expression is evaluated once. - Any
WHEREclause onDUALeither keeps that one row or removes it, which gives you a zero-row or one-row result.
SELECT 'one row' AS result FROM dual;
SELECT 'no rows' AS result FROM dual WHERE 1 = 0;The first statement returns one row. The second returns "no rows selected".
Reading the current date and time
SYSDATE is a function that takes no parentheses. It returns the current date and time of the database server's operating system as a DATE, which holds a time down to the second.
SELECT SYSDATE AS server_now FROM dual;The output looks like 28-NOV-26 in many default setups. That is only the display format, set by the session parameter NLS_DATE_FORMAT. The value still contains the time. To see it, convert the date to text with TO_CHAR:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text FROM dual;Oracle date arithmetic works in days. Adding 1 moves one day forward, and fractions of a day give hours and minutes.
SELECT SYSDATE AS now_value,
TRUNC(SYSDATE) AS midnight_today,
TRUNC(SYSDATE) + 1 AS midnight_tomorrow,
SYSDATE - 1/24 AS one_hour_ago,
ADD_MONTHS(TRUNC(SYSDATE), 1) AS same_day_next_month
FROM dual;TRUNC on a date removes the time portion and sets it to 00:00:00. Without TO_CHAR, SQL*Plus and many tools show only the date part, so use TO_CHAR whenever you want to check the time.
SYSDATE and its relatives
Oracle has several functions that return the current time. They differ in data type and in which clock or time zone they use.
| Function | Returns | Time zone basis |
|---|---|---|
| `SYSDATE` | `DATE` | Database server operating system |
| `SYSTIMESTAMP` | `TIMESTAMP WITH TIME ZONE`, with fractional seconds | Database server operating system |
| `CURRENT_DATE` | `DATE` | Session time zone |
| `CURRENT_TIMESTAMP` | `TIMESTAMP WITH TIME ZONE` | Session time zone |
You can see the differences side by side:
SELECT SESSIONTIMEZONE AS session_tz,
TO_CHAR(SYSDATE, 'HH24:MI:SS') AS sysdate_time,
TO_CHAR(CURRENT_DATE, 'HH24:MI:SS') AS current_date_time,
SYSTIMESTAMP AS sys_ts,
CURRENT_TIMESTAMP AS current_ts
FROM dual;If your session time zone matches the server's, the date values look the same. They diverge when a client in another region connects and sets its own time zone.
Drawing values from a sequence
A sequence is a schema object that generates numbers. You read it through two pseudocolumns:
sequence_name.NEXTVALincrements the sequence and returns the new value.sequence_name.CURRVALreturns the value most recently returned byNEXTVALin your own session.
A pseudocolumn behaves like a column but belongs to no table. Because it must be selected from some row source, DUAL is the usual choice for testing a sequence by hand.
CREATE SEQUENCE demo_seq START WITH 1 INCREMENT BY 1;
SELECT demo_seq.NEXTVAL AS next_id FROM dual;
SELECT demo_seq.CURRVAL AS current_id FROM dual;The first query returns 1. The second also returns 1, because it only reads the value your session already fetched.
Rules and gotchas for NEXTVAL
CURRVAL before NEXTVAL fails. CURRVAL is tracked per session. If you have not called NEXTVAL in the current session, Oracle raises ORA-08002: sequence DEMO_SEQ.CURRVAL is not yet defined in this session. This happens even if other sessions have used the sequence many times.
One increment per statement row, not per reference. Within a single row of a single statement, every reference to NEXTVAL returns the same value:
SELECT demo_seq.NEXTVAL AS a, demo_seq.NEXTVAL AS b FROM dual;Both columns show the same number, and the sequence advances only once. With DUAL there is one row, so one increment. If you select NEXTVAL from a table with 100 rows, the sequence advances 100 times. This is why the one-row guarantee of DUAL matters.
Gaps are normal. Rolling back a transaction does not return sequence numbers. A sequence also may skip values if the database caches them (the default cache is 20) and the instance restarts. Do not use a sequence when you need numbers without gaps.
Some clauses reject NEXTVAL. You cannot use it in a subquery, in a SELECT with DISTINCT, GROUP BY or ORDER BY, or in a view's query. The error is ORA-02287: sequence number not allowed here. For example, this fails:
SELECT DISTINCT demo_seq.NEXTVAL FROM dual;You often do not need DUAL for sequences. In an INSERT, use the sequence directly, since the VALUES clause needs no row source. From Oracle Database 11g, PL/SQL can assign it directly:
DECLARE
v_id NUMBER;
BEGIN
v_id := demo_seq.NEXTVAL;
DBMS_OUTPUT.PUT_LINE('Got ' || v_id);
END;
/Before 11g, PL/SQL needed SELECT demo_seq.NEXTVAL INTO v_id FROM dual;. You will still see that form in older code, and it still works.
Clean up when you are done:
DROP SEQUENCE demo_seq;Quick tests with DUAL
Because DUAL returns one row, you can check a function or expression without picking a real table. This is the quickest way to learn how a function behaves.
SELECT ROUND(123.456, 1) AS rounded,
SUBSTR('Oracle SQL', 1, 6) AS first_six,
INSTR('banana', 'an') AS first_an,
MONTHS_BETWEEN(DATE '2026-12-01', DATE '2026-09-01') AS months_apart,
TO_DATE('2026-02-28', 'YYYY-MM-DD') + 1 AS next_day
FROM dual;DATE '2026-12-01' is a date literal. It always uses the YYYY-MM-DD form and does not depend on NLS_DATE_FORMAT, which makes it safer than a bare string.
Testing NULL behavior
Quick tests are a good way to confirm how NULL works.
SELECT NVL(NULL, 'fallback') AS nvl_result,
NULL + 5 AS null_plus_five,
LENGTH('') AS empty_length,
'a' || NULL || 'b' AS concat_with_null
FROM dual;The results are worth noting:
NVLreturns'fallback'.NULL + 5is NULL, because arithmetic with NULL gives NULL.LENGTH('')is NULL, because Oracle treats an empty string as NULL.- The concatenation returns
ab, because||ignores NULL operands.
Testing errors
You can also trigger errors on purpose to see how Oracle reports them:
SELECT 1 / 0 AS boom FROM dual;Oracle raises ORA-01476: divisor is equal to zero. Testing the case outside your application tables is safer than discovering it in production.
Generating a set of rows
DUAL has one row, but a hierarchical query clause can multiply it. This is a common way to generate a number list or a calendar:
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 5;This returns the numbers 1 to 5. A date series works the same way:
SELECT TRUNC(SYSDATE) + LEVEL - 1 AS day_value
FROM dual
CONNECT BY LEVEL <= 7;The query returns today and the next six days. Without a FETCH FIRST or a CONNECT BY limit, a statement like this can run until it exhausts resources, so always bound it.
Checking a condition
Because a WHERE on DUAL returns one row or none, you can use it to turn a condition into a result:
SELECT 'yes' AS is_weekend
FROM dual
WHERE TO_CHAR(SYSDATE, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN');The query returns yes on Saturday and Sunday, and no rows on other days. The explicit NLS_DATE_LANGUAGE makes the day abbreviation independent of the session language.
Do not modify DUAL
DUAL belongs to the user SYS and is made available to everyone through a public synonym. Oracle depends on it containing exactly one row. Inserting, deleting or updating rows in DUAL can break statements across the database, including internal ones. Do not do it, and do not create your own table named DUAL in an application schema, since your table would shadow the real one for that schema.
FROM dual in Oracle Database 23ai
Oracle Database 23ai accepts a SELECT with no FROM clause. SELECT 1 + 1 AS two; works there, and raises an error in earlier releases. FROM dual still works in 23ai, so scripts that use it remain portable across versions. Keep it in code that has to run on older databases.
Common errors summary
| Symptom | Likely cause |
|---|---|
| Time missing from `SYSDATE` output | Display format hides it. Use `TO_CHAR` with an hour format |
| `ORA-08002` on `CURRVAL` | `NEXTVAL` not called yet in this session |
| `ORA-02287` | `NEXTVAL` used with `DISTINCT`, `GROUP BY`, `ORDER BY` or in a subquery |
| `ORA-00923: FROM keyword not found where expected` | Missing `FROM dual` on a release before 23ai |
| Sequence skips numbers | Rollback, caching, or an instance restart |
Exercise
A colleague runs SELECT s.NEXTVAL, s.NEXTVAL FROM dual; and expects the two columns to show 1 and 2. What do they actually get, and why?
Show answer
Both columns show the same value, for example 1 and 1. Within one row of one statement, every reference to NEXTVAL for a given sequence returns the same value, and the sequence is incremented once. DUAL supplies only one row, so the increment happens once.
Exercise
Write a query against DUAL that returns tomorrow at midnight as text in the form YYYY-MM-DD HH24:MI:SS, and say which function removes the time portion.
Show answer
SELECT TOCHAR(TRUNC(SYSDATE) + 1, 'YYYY-MM-DD HH24:MI:SS') AS tomorrowmidnight FROM dual; TRUNC removes the time portion by setting it to 00:00:00, and adding 1 moves the date forward one day.
Exercise
You run SELECT my_seq.CURRVAL FROM dual; in a fresh session on a sequence other users have been using all day. What happens, and how do you fix it?
Show answer
Oracle raises ORA-08002 because CURRVAL is tracked per session and NEXTVAL has not been called in this session. Call SELECT my_seq.NEXTVAL FROM dual; first, after which CURRVAL returns that value.