天天看点

第100章 SQL函数 NULLIF

文章目录

  • 第100章 SQL函数 NULLIF
  • 大纲
  • 参数
  • 描述
  • NULL 处理函数比较
  • 示例

第100章 SQL函数 NULLIF

如果两个表达式具有相同的值,则返回

NULL

的函数。

大纲

NULLIF(expression1,expression2)
           

参数

  • expression1

    - 表达式,可以是列名、数字或字符串文字、主变量或另一个标量函数的结果。
  • expression2

    - 表达式,可以是列名、数字或字符串文字、主变量或另一个标量函数的结果。

NULLIF

返回与

expression1

相同的数据类型。

描述

如果

expression1

的值等于

expression2

的值,则

NULLIF

函数返回

NULL

。否则,它返回

expression1

值。

NULLIF

等价于:

SELECT CASE 
WHEN value1 = value2 THEN NULL
ELSE value1
END
FROM MyTable
           

NULL 处理函数比较

SQL Function

Comparison

Test Return Value

IFNULL(ex1,ex2) [two-argument form] ex1 = NULL True returns ex2 False returns NULL
IFNULL(ex1,ex2,ex3) [three-argument form] ex1 = NULL True returns ex2 False returns ex3
{fn IFNULL(ex1,ex2)} ex1 = NULL True returns ex2 False returns ex1
ISNULL(ex1,ex2) ex1 = NULL True returns ex2 False returns ex1
NVL(ex1,ex2) ex1 = NULL True returns ex2 False returns ex1
NULLIF(ex1,ex2) ex1 = ex2 True returns NULL False returns ex1
COALESCE(ex1,ex2,…) ex = NULL for each argument True tests next ex argument. If all ex arguments are True (NULL), returns NULL. False returns ex

示例

SELECT Name,Age,NULLIF(Age,20) AS Nulled20
FROM Sample.Person