Sub topic 2
Calling Single-Row Functions
This chapter assumes you know basic SELECT ... FROM syntax, can connect to an Oracle Database session, and understand the data types NUMBER, VARCHAR2 and DATE.
Calling single-row functions in a SELECT list
A single-row function takes one value per row and returns one value per row. Examples include UPPER, ROUND, TO_CHAR and LENGTH. You can call them in the select list or in a WHERE clause.
SELECT UPPER(last_name) AS last_name_upper,
ROUND(salary / 12, 2) AS monthly_salary,
LENGTH(first_name) AS name_length
FROM employees
WHERE department_id = 50;Using DUAL for SYSDATE, sequences and quick tests
DUAL is a special table with one row and one column named DUMMY. When an expression does not need a table, select it FROM dual so the query returns exactly one row.
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text,
UPPER('oracle') AS upper_text,
2 + 3 * 4 AS arithmetic
FROM dual;A sequence generates unique numbers. Each call to NEXTVAL returns the next value. You need the CREATE SEQUENCE privilege to run the first statement.
CREATE SEQUENCE order_seq START WITH 1 INCREMENT BY 1;
SELECT order_seq.NEXTVAL AS next_id FROM dual;Filtering rows with WHERE
WHERE tests a condition on each row. Only rows where the condition is TRUE appear in the result. Combine conditions with AND and OR.
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = 50
AND salary > 3000;Exercise
Write a query on DUAL that returns the current date as YYYY-MM-DD in a column named today.
Show answer
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS today FROM dual;