比如数据库有2列 userid username
想查询这2列 其中 如果username为null 则用'-'代替select case username when () then '-' else username from table()中的要怎么填?  我试了好几种方式都不对求教~~~~~~

解决方案 »

  1.   


    select case when username is null then '-' else username end from table
      

  2.   

    select isnull(userid,'-'),username
    from tb
      

  3.   

    select isnull(username,'-') username from table
      

  4.   

    CREATE TABLE #temp
    (
    userid INT IDENTITY,
    username VARCHAR(10)
    )
    INSERT #temp
    SELECT 'a' UNION ALL SELECT NULL
    go
    --SQL:
    SELECT 
    userid,
    username = CASE WHEN username IS NULL THEN '-' ELSE username END
    FROM #temp
    /*
    1 a
    2 -
    */
      

  5.   

    select userid,isnull(username,'-')
    from table
      

  6.   

    select
      case when username is null then '-' else  username end
    from
     table
    select 
      isnull(username,'-')
    from
      table
      

  7.   

    select userid, username = case when null then '-' else username end  from T
      

  8.   


      select 
      userid,
       case when username IS null 
      then '-' 
      else username 
      end
      from tb
      
      
       select 
      userid,
       ISNULL(username,'-')
      from tb
      

  9.   

     select 
      userid,
       case when username IS null 
      then '-' 
      else username 
      end
      from tb
      
      
       select 
      userid,
       ISNULL(username,'-')
      from tb
      

  10.   

    select (case when username is null then '-' else username end) as UserName from table