如何检查 SQL Server 文本列是否为空?

发布于 2024-07-04 02:17:21 字数 198 浏览 5 评论 0原文

我正在使用 SQL Server 2005。我有一个带有文本列的表,表中有很多行,其中该列的值不为空,但它是空的。 尝试与 '' 进行比较会产生以下响应:

数据类型 text 和 varchar 在不等于运算符中不兼容。

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

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 compare against '' yields this response:

The data types text and varchar are incompatible in the not equal to operator.

Is there a special function to determine whether the value of a text column is not null but empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

你的背包 2024-07-11 02:17:22
DECLARE @temp as nvarchar(20)

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

SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)
DECLARE @temp as nvarchar(20)

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

SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)
最舍不得你 2024-07-11 02:17:22

我将针对 SUBSTRING(textColumn, 0, 1) 进行测试

I would test against SUBSTRING(textColumn, 0, 1)

草莓味的萝莉 2024-07-11 02:17:22

试试这个:

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

我希望能帮助你!

try this:

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

i hope help u!

椵侞 2024-07-11 02:17:22

您必须执行以下两项操作:

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

You have to do both:

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

故人的歌 2024-07-11 02:17:22

我知道这个问题有很多替代方案的答案,但我只是想将@Eric Z Beard 和 @Eric Z Beard 找到的最佳解决方案放在一起。 @Tim Cooper 与@Enrique Garcia & @乌利·科勒。

如果需要处理这样的事实,即在您的用例场景中,仅空格可能与空相同,因为下面的查询将返回 1,而不是 0。

SELECT datalength(' ')

因此,我会选择类似的内容:

SELECT datalength(RTRIM(LTRIM(ISNULL([TextColumn], ''))))

I know there are plenty answers with alternatives to this problem, but I just would like to put together what I found as the best solution by @Eric Z Beard & @Tim Cooper with @Enrique Garcia & @Uli Köhler.

If needed to deal with the fact that space-only could be the same as empty in your use-case scenario, because the query below will return 1, not 0.

SELECT datalength(' ')

Therefore, I would go for something like:

SELECT datalength(RTRIM(LTRIM(ISNULL([TextColumn], ''))))
风吹雪碎 2024-07-11 02:17:22

null 和空字符串等价吗? 如果是的话,我会在我的应用程序中包含逻辑(或者如果应用程序是“开箱即用的”,则可能是一个触发器?)以强制该字段为 null 或 '',但不是另一个。 如果您使用 '',那么您也可以将该列设置为 NOT NULL。 只是数据清洁度的问题。

Are null and an empty string equivalent? If they are, I would include logic in my application (or maybe a trigger if the app is "out-of-the-box"?) to force the field to be either null or '', but not the other. If you went with '', then you could set the column to NOT NULL as well. Just a data-cleanliness thing.

无所谓啦 2024-07-11 02:17:22

如果该值为 null 或为空,我希望显示一个预定义的文本(“没有可用的实验室”),我的朋友帮我解决了这个问题:

StrengthInfo = CASE WHEN ((SELECT COUNT(UnitsOrdered) FROM [Data_Sub_orders].[dbo].[Snappy_Orders_Sub] WHERE IdPatient = @PatientId and IdDrugService = 226)> 0)
                            THEN cast((S.UnitsOrdered) as varchar(50))
                    ELSE 'No Labs Available'
                    END

I wanted to have a predefined text("No Labs Available") to be displayed if the value was null or empty and my friend helped me with this:

StrengthInfo = CASE WHEN ((SELECT COUNT(UnitsOrdered) FROM [Data_Sub_orders].[dbo].[Snappy_Orders_Sub] WHERE IdPatient = @PatientId and IdDrugService = 226)> 0)
                            THEN cast((S.UnitsOrdered) as varchar(50))
                    ELSE 'No Labs Available'
                    END
时间海 2024-07-11 02:17:22

它会做两件事:

  1. 空值检查和字符串空值检查
  2. 将空值替换为默认值,例如 NA。
SELECT coalesce(NULLIF(column_name,''),'NA') as 'desired_name') from table;

It will do two things:

  1. Null check and string null check
  2. Replace empty value to default value eg NA.
SELECT coalesce(NULLIF(column_name,''),'NA') as 'desired_name') from table;
梦在夏天 2024-07-11 02:17:21

仅获取空值(而非空值):

SELECT * FROM myTable WHERE myColumn = ''

同时获取空值和空值:

SELECT * FROM myTable WHERE myColumn IS NULL OR myColumn = ''

仅获取空值:

SELECT * FROM myTable WHERE myColumn IS NULL

获取除空值和空值之外的值:

SELECT * FROM myTable WHERE myColumn <> ''

请记住,仅在必要时才使用 LIKE 短语,因为与其他类型的搜索相比,它们会降低性能。

To get only empty values (and not null values):

SELECT * FROM myTable WHERE myColumn = ''

To get both null and empty values:

SELECT * FROM myTable WHERE myColumn IS NULL OR myColumn = ''

To get only null values:

SELECT * FROM myTable WHERE myColumn IS NULL

To get values other than null and empty:

SELECT * FROM myTable WHERE myColumn <> ''

And remember use LIKE phrases only when necessary because they will degrade performance compared to other types of searches.

少女的英雄梦 2024-07-11 02:17:21

实际上,您只需使用 LIKE 运算符即可。

SELECT * FROM mytable WHERE mytextfield LIKE ''

Actually, you just have to use the LIKE operator.

SELECT * FROM mytable WHERE mytextfield LIKE ''
影子是时光的心 2024-07-11 02:17:21
ISNULL(
case textcolum1
    WHEN '' THEN NULL
    ELSE textcolum1
END 
,textcolum2) textcolum1
ISNULL(
case textcolum1
    WHEN '' THEN NULL
    ELSE textcolum1
END 
,textcolum2) textcolum1
眼泪淡了忧伤 2024-07-11 02:17:21
where datalength(mytextfield)=0
where datalength(mytextfield)=0
浅听莫相离 2024-07-11 02:17:21

使用 DATALENGTH 方法,例如:

SELECT length = DATALENGTH(myField)
FROM myTABLE

Use DATALENGTH method, for example:

SELECT length = DATALENGTH(myField)
FROM myTABLE
昔梦 2024-07-11 02:17:21

不要使用 isnull,而是使用 case,因为从性能角度来看,case 更好。

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

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
清风不识月 2024-07-11 02:17:21

我知道这篇文章很古老,但是我发现它很有用。

它没有解决我返回带有非空文本字段的记录的问题,所以我想我会添加我的解决方案。

这是对我有用的 where 子句。

WHERE xyz LIKE CAST('% %' as text)

I know this post is ancient but, I found it useful.

It didn't resolve my issue of returning the record with a non empty text field so I thought I would add my solution.

This is the where clause that worked for me.

WHERE xyz LIKE CAST('% %' as text)
时光礼记 2024-07-11 02:17:21

使用 IS NULL 运算符:

Select * from tb_Employee where ename is null

Use the IS NULL operator:

Select * from tb_Employee where ename is null
荒岛晴空 2024-07-11 02:17:21
SELECT * FROM TABLE
WHERE ISNULL(FIELD, '')=''
SELECT * FROM TABLE
WHERE ISNULL(FIELD, '')=''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文