NULLIF()、NULLIFZERO()、 ZEROIFNULL()用法

原文:

nullif(expr1,expr2)
Purpose: Returns  NULL if the two specified arguments are equal. If the specified arguments are not equal, returns the value of  expr1. The data types of the expressions must be compatible, according to the conversion rules from  Data Types. You cannot use an expression that evaluates to  NULL for  expr1; that way, you can distinguish a return value of  NULL from an argument value of  NULL, which would never match  expr2.

Usage notes: This function is effectively shorthand for a CASE expression of the form:

CASE
  WHEN expr1 = expr2 THEN NULL
  ELSE expr1
END

It is commonly used in division expressions, to produce a NULL result instead of a divide-by-zero error when the divisor is equal to zero:

select 1.0 / nullif(c1,0) as reciprocal from t1;

You might also use it for compatibility with other database systems that support the same NULLIF() function.

Return type: same as the initial argument value, except that integer values are promoted to BIGINT and floating-point values are promoted to DOUBLE; use CAST() when inserting into a smaller numeric column

Added in: Impala 1.3.0

翻译:

用途:返回NULL如果指定的两个参数是相等的。如果指定的参数不相等,返回expr1的价值。表达式的数据类型必须一致,根据数据类型的转换规则。您不能使用一个表达式expr1评估为NULL;这样,你可以区分的返回值为空的参数值NULL,这永远不会匹配expr2。

CASE
  WHEN expr1 = expr2 THEN NULL
  ELSE expr1
END

部门中常用表达式,生产一个空的结果,而不是被零除错误当除数等于零:

你也可以用它来与其他数据库系统的兼容性,支持相同的NULLIF()函数。

select 1.0 / nullif(c1,0) as reciprocal from t1;
返回类型:初始参数值一样,除了整数值提升为BIGINT和浮点值提升到两倍;使用CAST()当插入到一个较小的数字列。

原文:

nullifzero(numeric_expr)

Purpose: Returns NULL if the numeric expression evaluates to 0, otherwise returns the result of the expression.

Usage notes: Used to avoid error conditions such as divide-by-zero in numeric calculations. Serves as shorthand for a more elaborate CASE expression, to simplify porting SQL with vendor extensions to Impala.

Return type: same as the initial argument value, except that integer values are promoted to BIGINT and floating-point values are promoted to DOUBLE; use CAST() when inserting into a smaller numeric column

Added in: Impala 1.3.0

翻译:

用途:返回NULL如果数值表达式的求值结果为0,否则返回表达式的结果。
使用notes:用于避免在数值计算错误条件,如被零除。作为速记更复杂的表达式,简化移植与供应商扩展SQL黑斑羚。
返回类型:初始参数值一样,除了整数值提升为BIGINT和浮点值提升到两倍;使用CAST()当插入到一个较小的数字列

原文:

zeroifnull(numeric_expr)

Purpose: Returns 0 if the numeric expression evaluates to NULL, otherwise returns the result of the expression.

Usage notes: Used to avoid unexpected results due to unexpected propagation of NULL values in numeric calculations.  Serves as shorthand for a more elaborate CASEexpression, to simplify porting SQL with vendor extensions to Impala.

Return type: same as the initial argument value, except that integer values are promoted to BIGINT and floating-point values are promoted to DOUBLE; use CAST() when inserting into a smaller numeric column

翻译:

用途:返回0如果数值表达式的求值结果为空,否则返回表达式的结果。
使用注意:用于避免意想不到的结果由于意想不到的传播NULL值的数值计算。作为更复杂的CASEexpression速记,简化移植与供应商扩展SQL黑斑羚。
返回类型:初始参数值一样,除了整数值提升为BIGINT和浮点值提升到两倍;使用CAST()当插入到一个较小的数字列。

<think>好的,用户想了解SQL中的NULLIF函数的使用方法。首先,我需要回忆一下这个函数的基本定义和用途。NULLIF函数通常用于比较两个表达式,如果它们相等,则返回NULL,否则返回第一个表达式的结果。这和CASE语句中的某个条件类似,比如CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END。 接下来,我需要确认用户可能的疑问点。用户可能想知道语法结构、具体应用场景以及和其他函数的区别。比如,NULLIF与IFNULL或COALESCE有什么不同。根据提供的引用,比如引用[3]提到NULLIF等同于一个CASE语句,而引用[4]展示了如何结合NULLIF和COALESCE处理空字符串和NULL的情况。 然后,我需要组织回答的结构。先解释函数定义,给出语法,再举例说明。比如,引用中的示例-3显示当两个参数相等时返回NULL,否则返回第一个参数。另外,引用[2]提到在Spark SQL中使用NULLIF来处理字段置空的情况,这可以作为一个例子。 还要注意用户提到的不同数据库的支持情况,比如MySQL、PostgreSQL、Spark SQL等是否都支持NULLIF。虽然用户的问题没有明确指定数据库,但可能需要一般性的回答,或者指出不同数据库可能有差异。 另外,用户提供的引用中有关于NULLIF和COALESCE结合使用的例子,比如处理空字符串转换为NULL,然后用COALESCE替换为默认值。这样的实际应用案例对用户理解会很有帮助,应该包含在回答中。 需要确保回答的准确性,比如指出NULLIF的参数数量是两个,而IFNULL也是两个参数,但功能不同。还要注意区分空字符串和NULL是不同的概念,如引用[1]中提到的,空字符串''不是NULL,这在示例中需要明确说明。 最后,生成相关问题时要覆盖常见的使用场景、与其他函数的对比、性能考虑以及实际应用中的技巧。比如,如何用NULLIF避免除零错误,这在数学运算中很常见,是一个经典用例。 检查是否有遗漏的信息,比如错误处理或特定数据库的注意事项。确保回答符合用户的要求,使用中文,结构清晰,并正确引用提供的资料。</think>### NULLIF函数使用说明 NULLIF函数是SQL中用于条件比较的**空值处理函数**,主要用于通过比较两个表达式来生成NULL值。其核心逻辑为:**当两个输入值相等时返回NULL,否则返回第一个表达式的结果**。 --- #### 一、基本语法 ```sql NULLIF(expression1, expression2) ``` - **等效逻辑**:等价于 `CASE WHEN expression1 = expression2 THEN NULL ELSE expression1 END`[^3] --- #### 二、典型应用场景 1. **避免无效计算** ```sql SELECT 10 / NULLIF(column, 0) AS result; -- 避免除零错误 ``` 若`column`值为0,则`NULLIF(column, 0)`返回NULL,最终结果为NULL,而非报错。 2. **标准化空值与NULL** 结合`COALESCE`函数处理空字符串: ```sql SELECT COALESCE(NULLIF(columnA, ''), '0') AS normalized_value; -- 将空字符串转为'0'[^4] ``` 3. **字段置空逻辑** 在Spark SQL中实现字段置空: ```sql SELECT NULLIF(1,1) AS A; -- 字段A显示为NULL[^2] ``` --- #### 三、与其他函数的区别 | 函数 | 参数数量 | 行为描述 | |--------------|----------|---------------------------------------| | `NULLIF` | 2 | 两值相等返回NULL,否则返回第一个值 | | `IFNULL` | 2 | 第一值为NULL时返回第二值,否则返回第一值[^1] | | `COALESCE` | N | 返回第一个非NULL参数 | --- #### 四、注意事项 1. **数据类型一致性**:两个输入表达式需为相同或兼容类型 2. **空字符串与NULL**:空字符串`''`不被视为NULL 3. **返回值类型**:若两表达式类型不同,可能触发隐式转换 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值