ISNULL是判断是否为NULL
而NULLIF是把值换成NULL
COALESCE是用别的来代替NULL
SELECT employee_id,first_name,last_name,NULLIF (SALES_QUOTA,-1) as Quota
FROM employees
就是把-1变成 NULL
COALESCE(表达式1,表达式2,....表达式n)
从前到后,谁不是NULL就显示谁
Select employee_id,first_name,last_name,
COALESCE ( appt_quota,(Select Min(appt_quota) From employees),0 ) AS quota
From employees
Where department = ‘Marketing‘
附:
ISNULL(check_expression, replacement_value)
- check_expression 与 replacement_value 数据类型必须一致
- 如果 check_expression 为 NULL,则返回 replacement_value
- 如果 check_expression 不为 NULL,则返回 check_expression
NULLIF 用于检查两个表达式,语法:
NULLIF(expression, expression)
- 如果两个 expression 相等,则返回 NULL,该 NULL 为第一个 expression 的数据类型
- 如果两个 expression 不相等,则返回第一个 expression
例子1:
create table test_table( IDNUM NUMBER, CAT VARCHAR2(200), PRICE NUMBER);INSERT INTO test_table VALUES(1,‘‘,10);INSERT INTO test_table VALUES(2,‘cat1‘,10);INSERT INTO test_table VALUES(3,‘cat1‘,3);INSERT INTO test_table VALUES(4,‘cat2‘,11);INSERT INTO test_table VALUES(5,‘cat2‘,10);INSERT INTO test_table VALUES(6,‘‘,10);select COALESCE(CAT,‘unknown‘), SUM(PRICE)from test_table GROUP BY CAT;
result:
COALESCE(CAT,‘UNKNOWN‘) SUM(PRICE)
unknown 20
cat1 13
cat2 21
注意:COALESCE 与 ISNULL相比,COALESCE 类似case,可以多个input,ISNULL只有2个parameter
COALESCE不能完成类型的转换,否则报错
时间: 2024-11-10 06:38:12