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

前端 未结 16 1117
粉色の甜心
粉色の甜心 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:37

    Use the IS NULL operator:

    Select * from tb_Employee where ename is null
    
    0 讨论(0)
  • 2020-12-07 11:39
    DECLARE @temp as nvarchar(20)
    
    SET @temp = NULL
    --SET @temp = ''
    --SET @temp = 'Test'
    
    SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)
    
    0 讨论(0)
  • 2020-12-07 11:42

    try this:

    select * from mytable where convert(varchar, mycolumn) = ''
    

    i hope help u!

    0 讨论(0)
  • 2020-12-07 11:44

    Actually, you just have to use the LIKE operator.

    SELECT * FROM mytable WHERE mytextfield LIKE ''
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-07 11:45

    You have to do both:

    SELECT * FROM Table WHERE Text IS NULL or Text LIKE ''

    0 讨论(0)
提交回复
热议问题