Sub topic 5
Calling Single-Row Functions in Oracle SQL
Assume you know basic SELECT statements, columns, rows, data types, and the DUAL table. This chapter focuses on functions that calculate one result for each input row.
What a single-row function does
A single-row function accepts a value from one row and returns one value for that row. If a query processes 20 rows, the function normally produces 20 results.
For example, UPPER changes lowercase letters to uppercase:
SELECT first_name,
UPPER(first_name) AS name_in_uppercase
FROM employees;The query returns the original first_name and a second value calculated from it. The function does not combine employees or reduce the number of rows.
This differs from an aggregate function such as COUNT, which combines values from several rows and normally returns one result for a group.
Common function categories
Oracle supplies many single-row functions. The most useful categories include:
- Character functions, such as
UPPER,LOWER,INITCAP,LENGTH, andSUBSTR. - Number functions, such as
ROUND,TRUNC, andMOD. - Date functions, such as
SYSDATE,ADD_MONTHS, andMONTHS_BETWEEN. - Conversion functions, such as
TO_CHAR,TO_DATE, andTO_NUMBER. - Null-handling functions, such as
NVLandCOALESCE.
You can test a function against a literal value by selecting it from DUAL:
SELECT LOWER('ORACLE') AS lower_text,
INITCAP('oracle sql') AS title_text,
LENGTH('Oracle') AS character_count,
ROUND(45.678, 2) AS rounded_number
FROM dual;This returns one row because DUAL contains one row. Each expression is evaluated independently.
Combining and nesting functions
The concatenation operator || joins character values. Functions can be used inside larger expressions:
SELECT first_name || ' ' || UPPER(last_name) AS display_name
FROM employees;You can also nest one function inside another. The inner function runs first, and its result becomes the input to the outer function:
SELECT first_name,
SUBSTR(UPPER(last_name), 1, 3) AS first_three_letters
FROM employees;Here, Oracle first converts last_name to uppercase. SUBSTR then returns the first three characters.
Formatting dates and numbers
TO_CHAR converts a date or number to text using a format model. A format model is a pattern that tells Oracle how to display the value.
SELECT last_name,
TO_CHAR(hire_date, 'YYYY-MM-DD') AS hired_on,
TO_CHAR(salary, 'FM$999,999,990.00') AS salary_text
FROM employees;The resulting values are text, even though hire_date and salary have other data types in the table. This matters when you sort or compare the results. For example, text values may sort alphabetically rather than numerically.
A function in the SELECT list changes the displayed result. It does not change the stored value in the table.
NULL and function results
Many functions return NULL when their input is NULL. For example, LENGTH(NULL) returns NULL. Use NVL when you need a replacement value:
SELECT last_name,
NVL(commission_pct, 0) AS commission_rate
FROM employees;This displays zero for employees whose commission percentage is unknown. The query still does not modify the table.
Exercise
Write a query against DUAL that returns the text ORACLE SQL in lowercase, its length, and the first four characters in uppercase.
Show answer
SELECT LOWER('ORACLE SQL') AS lowertext, LENGTH('ORACLE SQL') AS charactercount, SUBSTR(UPPER('ORACLE SQL'), 1, 4) AS first_four FROM dual;