Sub topic 6
Using DUAL for SYSDATE, sequences and quick tests
This chapter assumes you understand tables, rows, columns, basic Oracle data types, and how to run SQL in SQL*Plus, SQL Developer, or another Oracle SQL worksheet. You will use DUAL when a query needs one row but does not need to read an application table.
What DUAL provides
DUAL is an Oracle table with one column named DUMMY and one row containing the character value X. Because it has exactly one row, a query against it normally returns exactly one row.
SELECT * FROM dual;The result is:
| DUMMY |
|---|
| X |
The row is useful as a safe row source for evaluating expressions, calling functions, reading the current database time, or requesting a sequence value.
Reading the database date and time
SYSDATE is an Oracle date value representing the current date and time from the database server's operating system. It is evaluated when the statement runs. Format it with TO_CHAR when you need readable text.
SELECT SYSDATE AS server_date,
TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS server_time
FROM dual;The DATE value includes seconds, even though a client tool might display only the date. The format model controls the text returned by TO_CHAR. HH24 means a 24-hour clock, while MI means minutes.
SYSDATE describes the database host's clock. It can differ from the time zone configured on your workstation. For time-zone-aware applications, also investigate SYSTIMESTAMP and CURRENT_TIMESTAMP.
Getting sequence values
A sequence is an Oracle database object that generates numbers. It is commonly used for identifiers. NEXTVAL requests the next number from a sequence. Since NEXTVAL is selected from a row source, use DUAL when you only want to generate and inspect one value.
CREATE SEQUENCE order_seq
START WITH 1
INCREMENT BY 1;
SELECT order_seq.NEXTVAL AS next_id
FROM dual;Running the SELECT again returns the next value:
SELECT order_seq.NEXTVAL AS next_id
FROM dual;A sequence is independent of a table. Values can have gaps because a transaction may roll back after obtaining a number, or because the sequence cache advances. Do not treat sequence values as guaranteed gap-free row counts.
When inserting a row, request NEXTVAL as part of the insert rather than selecting it first and assuming it will remain yours:
INSERT INTO orders (order_id, order_date)
VALUES (order_seq.NEXTVAL, SYSDATE);The exact table and column names must match your application schema.
Quick expression tests
DUAL is also useful for testing an expression before placing it in a larger query. This avoids changing application data.
SELECT 2 + 3 AS total,
UPPER('oracle') AS upper_text,
'Ora' || 'cle' AS joined_text,
NVL(NULL, 'fallback') AS null_test
FROM dual;The || operator concatenates strings. UPPER converts text to uppercase. NVL returns its second argument when its first argument is NULL.
Version note
Most Oracle releases require a FROM clause for this style of query, which is why FROM dual is familiar. Oracle Database 23ai also allows a SELECT without FROM:
SELECT 1 + 1 AS two;Use FROM dual when your code must run on earlier releases, or when you want the intent to be clear to Oracle SQL users.
Exercise
Write a query against DUAL that returns the current server time as text in the format DD-MON-YYYY HH24:MI.
Show answer
SELECT TOCHAR(SYSDATE, 'DD-MON-YYYY HH24:MI') AS currenttime FROM dual;