Using Single-Row Functions to Customize Output
What this domain tests
Single-row functions return one result per input row. The exam tests character, numeric, date, and general functions, including nesting and NULL behavior.
Core rules
- Character:
UPPER,LOWER,INITCAPchange case;LENGTHcounts characters;SUBSTR(str, start, length)supports negative starts;INSTRreturns the position or 0;LPAD/RPADpad or truncate;TRIM,LTRIM,RTRIM, andREPLACEedit strings. - Numeric:
ROUND,TRUNC,MOD,ABS,CEIL,FLOOR,POWER.ROUNDandTRUNCaccept a precision that may be negative. - Date:
SYSDATEreturns current date and time. Date arithmetic uses days.MONTHS_BETWEEN,ADD_MONTHS,NEXT_DAY,LAST_DAY,ROUND, andTRUNCoperate on dates. - General:
NVL,NVL2,NULLIF,COALESCE,CASE, andDECODE. - Most functions propagate NULL; the general functions are the usual exceptions.
Worked example
SELECT last_name, UPPER(SUBSTR(last_name, 1, 3)) AS prefix, ADD_MONTHS(hire_date, 6) AS review_date FROM employees WHERE MONTHS_BETWEEN(SYSDATE, hire_date) > 120;
SUBSTR extracts the first three characters, and UPPER makes them uppercase. ADD_MONTHS adds six calendar months; if the target month is shorter, Oracle returns the last day of that month. MONTHS_BETWEEN returns a fractional number, so the comparison is numeric.
Traps
- Assuming
SUBSTRwith a negative start counts from the right but still reads forward; the direction changes. - Forgetting that
INSTRreturns 0, not NULL, when not found. - Using
TRIMwith the wrong syntax; it trims a character, not a string pattern. - Expecting
NVLto change the data type of the first argument; it uses the first argument's type. - Adding a number to a date expecting hours; date arithmetic is in days.