Sql Server - 在 where 子句中执行 RTRIM/LTRIM 的替代方法

标签 sql sql-server performance trim

我有一个 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 and HAVING 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/

相关文章:

sql-server - 数据库 View 性能问题

c++ - 如何提高我的 OpenMP 代码的性能?

java - 如何减少java程序的启动时间和内存消耗?

sql - 如果值为空则合并行

mysql - (Mysql 错误 : #1215 - Cannot add foreign key constraint) for my program

php - 选择以 % 作为值字符的查询 sql

SQL Server 如何获取每个类别下的新闻数量

sql - 如何获取SQL中不同列中不同值的计数

java - Hibernate SQL - 父子之间的中间表(不同类型)

.net - 软件带宽/数据库增长公式