我有一个 varchar 列名
我想过滤名称为空字符串的所有结果...
select name
from tblNames
where name <> ''
我想做的是:
select name
from tblNames
where Ltrim(RTrim(name)) <> ''
我想在 where 子句中对名称进行修剪,但我读过一些文章提到了 where 子句中函数的性能问题
我想要一个不影响性能的解决方案
最佳答案
SQL-Server 中的标准行为是这样的
' ' = ''
是 TRUE
,因为尾随空格被忽略。来自 MSDN support :
SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, , General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of
WHERE
andHAVING
clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings'abc'
and'abc '
to be equivalent for most comparison operations.The only exception to this rule is the
LIKE
predicate. ...
那么,你的情况WHERE name <> ''
应该可以正常工作,并且不包含任何只有空格的字符串。
关于Sql Server - 在 where 子句中执行 RTRIM/LTRIM 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112301/