How do I check if a SQL Server text column is empty?

前端 未结 16 1206
粉色の甜心
粉色の甜心 2020-12-07 11:07

I am using SQL Server 2005. I have a table with a text column and I have many rows in the table where the value of this column is not null, but it is empty. Trying to comp

16条回答
  •  无人及你
    2020-12-07 11:44

    Instead of using isnull use a case, because of performance it is better the case.

    case when campo is null then '' else campo end
    

    In your issue you need to do this:

    case when campo is null then '' else
      case when len(campo) = 0 then '' else campo en
    end
    

    Code like this:

    create table #tabla(
    id int,
    campo varchar(10)
    )
    
    insert into #tabla
    values(1,null)
    
    insert into #tabla
    values(2,'')
    
    insert into #tabla
    values(3,null)
    
    insert into #tabla
    values(4,'dato4')
    
    insert into #tabla
    values(5,'dato5')
    
    select id, case when campo is null then 'DATA NULL' else
      case when len(campo) = 0 then 'DATA EMPTY' else campo end
    end
    from #tabla
    
    drop table #tabla
    

提交回复
热议问题