简体   繁体   English

为什么忽略 SQL Server 2014 中的空格文本

[英]Why ignoring space text in SQL Server 2014

When I compared text like this:当我比较这样的文字时:

'a' = 'a '

the result is True - but I was expecting it to be false.结果是真——但我原以为它是假的。

Do you know why the result is true?你知道为什么结果是真的吗? And can I do something to solve this issue?我可以做些什么来解决这个问题吗?

You can use DATALENGTH()您可以使用DATALENGTH()

Example例子

SELECT * FROM TableName WHERE DATALENGT('a') = DATALENGTH('a ')

DATALENGT('a') return the result as 1 DATALENGT('a')将结果返回为 1
and DATALENGTH('a ') return the result as 2, includes the space text.DATALENGTH('a ')返回结果为 2,包括空格文本。

Please check the db<>fiddle for reference.请检查db<>fiddle以供参考。

The LIKE operator does not pad the trailing spaces: LIKE 运算符不填充尾随空格:

SELECT * FROM basePath WHERE 'a' LIKE 'a '

More information can be found here: https://dba.stackexchange.com/questions/56876/sql-server-auto-trim-of-varchar-value-in-equal-comparison-but-not-like-compariso更多信息可以在这里找到: https://dba.stackexchange.com/questions/56876/sql-server-auto-trim-of-varchar-value-in-equal-comparison-but-not-like-compariso

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM