SQL查询中
如果某值为0变为NULL有函数处理吗?
与isnull(xxx,0)相反的功能。

解决方案 »

  1.   

    case when col = 0 then null else col end 
      

  2.   

    NULLIF
    如果两个指定的表达式相等,则返回空值。语法
    NULLIF ( expression , expression )参数
    expression常量、列名、函数、子查询或算术运算符、按位运算符以及字符串运算符的任意组合。返回类型
    返回类型与第一个 expression 相同。如果两个表达式不相等,NULLIF 返回第一个 expression 的值。如果相等,NULLIF 返回第一个 expression 类型的空值。注释
    如果两个表达式相等且结果表达式为 NULL,NULLIF 等价于 CASE 的搜索函数。
      

  3.   

    create table tb(col int)
    insert into tb values(0)
    insert into tb values(0)
    insert into tb values(1)
    insert into tb values(2)
    insert into tb values(3)
    insert into tb values(0)
    insert into tb values(0)
    goselect col , newcol = (case when col = 0 then null else col end ) from tbdrop table tb/*
    col         newcol      
    ----------- ----------- 
    0           NULL
    0           NULL
    1           1
    2           2
    3           3
    0           NULL
    0           NULL(所影响的行数为 7 行)*/
      

  4.   


    case when a= 0 then null else a end
      

  5.   

    NULLIF(a,b)
    a b 如果相等 则值为NULL