SQL - 如何搜索没有文本的字段?

SQL - how to search for fields with no text?

我在 field1 中有数据,其中有 1-~50 个空白 spaces(不为空,但没有文本)。为了 trim,我需要搜索所有这些字段的方法。

*Select * from table1 where field1 = ' '* 不捕获多一个 space 的记录。使用 >= 或 <= 包括带有我不想更新的实际文本的记录。

有没有办法只捕获没有实际文本的记录?

在 sql 服务器尾随 space 被忽略。因此,您的查询将返回没有 spaces 或 50 spaces 的行。您是否只想要那些具有多个 space 的那些而排除那些只有一个 space 的?或者,如果他们只有 spaces,您就不需要他们。您可以为此使用替换。

select *
from table1
where replace(field1, ' ', 'x') = ''

是的,证明尾随 spaces 被忽略是非常容易的。

with SpacesDemo as
(
    select ' ' as Spaces, 1 as NumSpaces union all
    select '          ', 10
)

select *
from SpacesDemo
where Spaces = ''

我怀疑您的数据中有不属于 space 的字符。

您可以使用 like:

select t.*
from t
where field1 not like '%[^ ]%' and field1 like '% %';

您可以在字符 class 中包含您想要的任何字段,因此这很容易概括为您可能认为的任何内容 space。

可以像正则表达式一样使用 Like 运算符

select * from table1 where field1 LIKE '^[ ]{1,}'

在 Transact-SQL (SQLServer) 中有 TRIM 函数从开头删除白色-space 字符(或其他取决于参数)和字符串的结尾 .

您的查询应该看起来像这样:

SELECT * FROM table1 WHERE TRIM(field1) = '';

相似的功能都在Oracle and PostgreSQL