我使用的是SQL Server 2005。我有一个带有文本列的表,在表中有许多行,其中该列的值不是空的,但它是空的。试着对比”会得到这样的回答:

数据类型text和varchar在不等于操作符中是不兼容的。

是否有一个特殊的函数来确定文本列的值是否为空而不是空?


当前回答

DECLARE @temp as nvarchar(20)

SET @temp = NULL
--SET @temp = ''
--SET @temp = 'Test'

SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)

其他回答

我将测试SUBSTRING(textColumn, 0,1)

where datalength(mytextfield)=0

实际上,你只需要使用LIKE操作符。

SELECT * FROM mytable WHERE mytextfield LIKE ''
DECLARE @temp as nvarchar(20)

SET @temp = NULL
--SET @temp = ''
--SET @temp = 'Test'

SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)

用一个case代替isnull,因为它的性能更好。

case when campo is null then '' else campo end

在你的问题,你需要这样做:

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

代码如下:

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