小妹现在在做一个页面显示的东西,如果变量等于0,就显示为空白,如果变量不等于0,则显示正常的数字我本来的代码是,replace(isnull(变量,0),0,'')这样的执行结果是,如果变量等于0的话,什么都不显示,但是如果变量等于10,100,1000……的时候,
后面的0也都没有了有什么办法可以解决吗?is……的其他我也试过了,还是没好用完全菜鸟,各位大侠救救急啊!

解决方案 »

  1.   

    isnull(cast(变量 as varchar),'')
      

  2.   

    try:case when isnull(变量,0)<>0 then rtrim(变量) else '' end 
      

  3.   

    create table tb(id int , val int)
    insert into tb values(1 , 10)
    insert into tb values(2 , 20)
    insert into tb values(3 , null)
    insert into tb values(4 , null)
    insert into tb values(5 , 50)
    goselect id , isnull(cast(val as varchar),'') val from tbdrop table tb/*
    id          val                            
    ----------- ------------------------------ 
    1           10
    2           20
    3           
    4           
    5           50(所影响的行数为 5 行)*/
      

  4.   

    case @a when '' else @a end
      

  5.   

    case @a when '' else @a end
      

  6.   


    接小妹分if object_id('tb')is not null drop table tb
    go
    create table tb(id int , val int)
    insert into tb values(1 , 10)
    insert into tb values(2 , 0)
    insert into tb values(3 , 0)
    insert into tb values(4 , null)
    insert into tb values(5 , 50)
    goselect id ,isnull(nullif(rtrim(val),'0'),'') val from tb
    /*
    id          val
    ----------- ------------
    1           10
    2           
    3           
    4           
    5           50(5 row(s) affected)*/
      

  7.   


    select id,val = (case when val=0 or val is null then '' else rtrim(val) end)
    from tb
    /*
    id          val
    ----------- ------------
    1           10
    2           
    3           
    4           
    5           50(5 row(s) affected)
    */