sql和null有关的函数

在写sql的时候,经常会遇到和null相关的问题,经常用到虑空函数。常见的有NVL,NULLIF,COALESCE函数,其他还有很多在此不介绍了。

  • 1.NVL----- NVL lets you replace null (returned as a blank) with a string in the results of a query. If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVLreturns expr1.

Description of nvl.gif follows

  •  2.NULLIF------NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns expr1. You cannot specify the literal NULL for expr1.
  • 类似于:CASE WHEN expr1 = expr 2 THEN NULL ELSE expr1 END

Description of nullif.gif follows

  • 3.COALESCE-----COALESCE  returns the first non-null expr in the expression list. At least one expr must not be the literal NULL. If all occurrences of expr evaluate to null, then the function returns null.  
  • 类似于:CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END
  • COALESCE (expr1, expr2, ..., exprn), for n>=3  类似于:CASE WHEN expr1 IS NOT NULL THEN expr1  ELSE COALESCE (expr2, ..., exprn) END

Description of coalesce.gif follows

  • 4.value函数也可以用来处理null
  • VALUE takes as its argument a correlation variable (table alias) associated with a row of an object table and returns object instances stored in the object table. The type of the object instances is the same type as the object table.

 Description of value.gif follows

总结一下:

nvl,表达式1 为空,就返回表达式2。

nullif,表达式 1和表达式2如果相等,返回null,不相等返回表达式1.

coalesce,返回表达式不为空项结束。例子:coalesce(手机号,电话,邮箱) as '联系方式'

VALUE(EXPRESSION1,EXPRESSION2)  表达式1为空,返回表达式2 

 

搬运工来自:SQL Functions  

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值