0% found this document useful (0 votes)
13 views

Null General Functions

The document describes several SQL functions for handling null values and conditional logic: NVL replaces null values with a specified value of the same data type. NVL2 returns one value if the first value is not null, and a different value if it is null. NULLIF returns null if two values are equal, otherwise it returns the first value. COALESCE returns the first non-null value from a list. CASE allows returning different values based on conditions. DECODE is similar to CASE but does not support an ELSE clause.

Uploaded by

MazMoh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Null General Functions

The document describes several SQL functions for handling null values and conditional logic: NVL replaces null values with a specified value of the same data type. NVL2 returns one value if the first value is not null, and a different value if it is null. NULLIF returns null if two values are equal, otherwise it returns the first value. COALESCE returns the first non-null value from a list. CASE allows returning different values based on conditions. DECODE is similar to CASE but does not support an ELSE clause.

Uploaded by

MazMoh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

GENERAL FUNCTIONS

NVL(value, replace with value of same datatype) NVL - if the value is NULL replace the value with, X ** data types must match in each list and can be number, char, varchar2 or date data types SELECT last_name, NVL(commission_pct, 10) FROM employees WHERE commission_pct IS NULL; SELECT last_name, NVL(hire_date, '10-FEB-02') FROM employees WHERE hire_date IS NULL; SELECT last_name, NVL(hire_date, TO_DATE('February 16, 2002', 'Month DD, YYY')) FROM employees WHERE hire_date IS NULL; SELECT last_name, NVL(first_name, 'James'') FROM employees WHERE first_name IS NULL; NVL2(value1, value2, value3) NVL2 - if the first value is not null, return value 2 if the first value is null, return value 3 SELECT department_name, manager_id, NVL2(manager_id, 'yes', 'no' ) AS "Has Manager" FROM departments

NULLIF(value1, value2) If the first value and the second value are equal return NULL Ifthe first value and the second value are not equal return first value
**remember NULLIFEQUAL

SELECT first_name, last_name, salary, NULLIF(salary,24000) AS "Salary Info" FROM employees WHERE last_name IN ('King', 'Kochhhar','Ernst') SELECT first_name, last_name, phone_number, NULLIF(SUBSTR(phone_number,1,3),'590') "Phone Null For 590 Area Codes" FROM employees; SELECT first_name, last_name, job_id, NULLIF(job_id,'ST_CLERK') title FROM employees; COALESCE(value1, value2, value3) COALESCE - from a list, return the first NOT NULL SELECT last_name, COALESCE(commission_pct, department_id, salary) info FROM employees WHERE employee_id = 100; SELECT last_name, COALESCE(commission_pct, manager_id, salary) info FROM employees WHERE employee_id = 100;

CASE - CASE X WHEN - THEN SELECT last_name, manager_id, salary, CASE manager_id WHEN 100 THEN salary*1.25 ELSE salary END AS "NEW SALARY" FROM employees; SELECT last_name, first_name, CASE first_name WHEN 'Neena' THEN 'needs raise' ELSE 'no raise' END AS "Raise Review" FROM employees WHERE last_name IN ('King', 'Kochhar'); SELECT last_name, first_name, hire_date, CASE hire_date WHEN (TO_DATE('JANUARY 13, 1993', 'Month DD, YYYY')) THEN (ADD_MONTHS(hire_date, 6)) ELSE (ADD_MONTHS(hire_date, 12)) END AS "Raise Review" FROM employees WHERE last_name IN ('De Haan', 'Ernst'); DECODE - form of IF-THEN but NO ELSE SELECT location_id, city, DECODE(TO_CHAR(location_id), TO_CHAR(1400), '2002', TO_CHAR(1500), '2003', TO_CHAR(1700), '2004', 'UNDER CONSTRUCTION')AS "Established" FROM locations

You might also like