请教sqlserver中除数为零的判断!   
  在数据库A中要用到A.e/A.f作为新字段A.g,     A.f有可能为零,   
  请问如何做使当A。f为零时,就不作除法运算。置A。g为O。   
  请教一个最简单的实现方法。谢谢。

解决方案 »

  1.   

    ISNULL(A.e/NULLIF(A.f,0)),0) AS g
      

  2.   


    declare @str int
    set @str=0
    select 6/isnull(@str,1)
      

  3.   


    declare @str int
    set @str=0
    select 6/isnull(@str,1)
      

  4.   


    select *,
    g = case when A.f = 0 then 0 else A.e/A.f end
    from A
      

  5.   

    update A set g=(case when f=0 then 0 when f<>0 then e/f end)
      

  6.   


    select case a.f when 0 then 0 else a.e/a.f end from tb
      

  7.   

    select isull(12/nullif(0,0),0)
    这个函数好用
      

  8.   

    DECLARE  @table TABLE(f1 numeric(18,2),f2 int,f3 numeric(18,2))--测试数据
    insert into @table (f1,f2,f3)
    select 12,5,null union all
    select 10,0,null union all
    select 89,null,null union all
    select 56,3,null--除法前
    select *
    from @table--做除法
    update @table
    set f3= case when isnull(f2,0)=0 then null else f1/f2 end--除法后
    select *
    from @table
      

  9.   


    (所影响的行数为 4 行)f1                   f2          f3                   
    -------------------- ----------- -------------------- 
    12.00                5           NULL
    10.00                0           NULL
    89.00                NULL        NULL
    56.00                3           NULL(所影响的行数为 4 行)
    (所影响的行数为 4 行)f1                   f2          f3                   
    -------------------- ----------- -------------------- 
    12.00                5           2.40
    10.00                0           NULL
    89.00                NULL        NULL
    56.00                3           18.67(所影响的行数为 4 行)