我使用的是SQL Server 2005。我有一个带有文本列的表,在表中有许多行,其中该列的值不是空的,但它是空的。试着对比”会得到这样的回答:
数据类型text和varchar在不等于操作符中是不兼容的。
是否有一个特殊的函数来确定文本列的值是否为空而不是空?
我使用的是SQL Server 2005。我有一个带有文本列的表,在表中有许多行,其中该列的值不是空的,但它是空的。试着对比”会得到这样的回答:
数据类型text和varchar在不等于操作符中是不兼容的。
是否有一个特殊的函数来确定文本列的值是否为空而不是空?
当前回答
用一个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
其他回答
使用DATALENGTH方法,例如:
SELECT length = DATALENGTH(myField)
FROM myTABLE
用一个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
试试这个:
select * from mytable where convert(varchar, mycolumn) = ''
希望能帮到你!
它会做两件事: 空检查和字符串空检查 将空值替换为默认值,例如NA。
SELECT coalesce(NULLIF(column_name,''),'NA') as 'desired_name') from table;
实际上,你只需要使用LIKE操作符。
SELECT * FROM mytable WHERE mytextfield LIKE ''