前幾篇文章提及過,當資料表數值為空值時有三種情況,分別為:
但不管是何種情況,在SQL當中空值相當於運算中的無限大,必須加以轉換後才能處理,而轉換函數又分為四種:
NVL(commission_pct,0)
解釋:如果commission_pct為空值,則輸出0。
NVL(hire_date,'01-Jan-2014')
解釋:如果hire_date為空值,則輸出'01-Jan-2014'。
SELECT last_name, salary, commission_pct,
salary*(1+NVL2(commission_pct, commission_pct,0))*12"年收入"
FROM employees
WHERE department_id IN (50,80);
執行結果如下:
SELECT first_name, LENGTH(first_name)"名字字數",
last_name, LENGTH(last_name)"姓氏字數",
NULLIF(LENGTH(first_name),LENGTH(last_name))比對結果
FROM employees;
執行結果如下:
SELECT last_name,employee_id,
COALESCE (TO_CHAR(commission_pct),TO_CHAR(manager_id),
'他是老闆')
FROM employees;
執行結果如下: