我的名字和姓氏带有尾随空格。我有两个 SQL 查询——即使我搜索没有空格,第一个查询也会返回结果。(第一个查询返回不需要的结果)。
- 这种行为在所有版本的 SQL Server 中是否一致?
- 这是一种已知的行为吗?它是否记录在
msdn
for 的任何地方SQL Server 2008 R2
?
代码
DECLARE @NameTable TABLE (first_name varchar(40),last_name varchar(40))
INSERT INTO @NameTable VALUES ('STEVEN ','STANLEY ');
--QUERY 1
SELECT first_name AS [FirstName], last_name AS [LastName]
FROM @NameTable A
WHERE (first_name = 'STEVEN')
AND (last_name = 'STANLEY')
--QUERY 2
SELECT first_name AS [FirstName], last_name AS [LastName]
FROM @NameTable A
WHERE (( ISNULL(first_name,'')+' ' +ISNULL(last_name,'') ) = 'STEVEN STANLEY')
--QUERY 3 (With LTRIM and RTRIM)
SELECT first_name AS [FirstName], last_name AS [LastName]
FROM @NameTable A
WHERE (( ISNULL(LTRIM(RTRIM(first_name)),'')+' ' +ISNULL(LTRIM(RTRIM(last_name)),'') ) = 'STEVEN STANLEY')
参考: