Using Conversion Functions and Conditional Expressions
What this domain tests
This domain covers explicit and implicit data conversion, format models, and conditional expressions used to transform output.
Core rules
TO_CHAR(number, 'fmt')andTO_CHAR(date, 'fmt')format values. Common date elements areYYYY,MM,DD,HH24,MI,SS,DAY,MON;FMremoves padding.TO_DATE(string, 'fmt')interprets a string.TO_NUMBER(string, 'fmt')converts numeric text.YYuses the current century.RRuses a sliding century: 00-49 maps to the current century, 50-99 to the previous century (relative to the current year).- Implicit conversion happens when types are mixed, but it depends on NLS settings and can prevent index use.
CASEsupports simple and searched forms and can return different expressions per condition.DECODE(expr, search1, result1, ..., default)performs equality tests and treats two NULLs as equal.NVL,NVL2,NULLIF, andCOALESCEhandle NULL substitution and comparison.
Worked example
SELECT TO_CHAR(hire_date, 'DD-MON-YYYY') AS hired, CASE WHEN salary >= 10000 THEN 'High' WHEN salary >= 5000 THEN 'Medium' ELSE 'Low' END AS band FROM employees;
The format model renders each date component. The searched CASE evaluates conditions in order; the first true condition wins, so a salary of 12000 is High and never reaches Medium. Without ELSE, unmatched rows return NULL.
Traps
- Using
YYfor birth dates near a century boundary;RRis often intended. - Writing
CASE salary WHEN NULL THEN ...; equality with NULL is unknown. UseWHEN salary IS NULL. - Forgetting that
DECODEconsiders NULL equal to NULL, unlikeCASE. - Omitting the format model and relying on NLS date formats.
- Assuming
TO_CHARchanges the stored data type; it only changes the displayed string.