SQL Server:与尾随空格不同

时间:2016-07-10 02:52:18

标签: sql-server

如何让这个例子显示('空格')和('空格')?

create table #example
( 
    myString varchar(50)
)

insert into #example values ('space'), ('space ')

select distinct * 
from #example

我知道SQL比较运算符认为这两个字符串是等价的,但我不是这种情况。

https://github.com/kennybatista/Security.me

https://support.microsoft.com/en-us/kb/316626

后续回答

注意:DATALENGTH(...)函数还可以生成对尾随空格敏感的连接:

select *
from table1
left join table2 on table1.id1 = table2.id1 
                 and datalength(table1.id1) = datalength(table2.id1)

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT A.myString
FROM (
    SELECT DISTINCT myString, DATALENGTH(myString) [DataLength]
    FROM #example
    ) A

有了这些数据:

insert into #example values ('space'),
('space '),
('space2'),
('space2');

结果:

myString
--------
space
space 
space2

答案 1 :(得分:0)

您可以通过删除select stmt ...中的distinct关键字来实现这一目标。

从#example

中选择myString

从#example

中选择*

但我想这个问题不仅仅是这个......?

编辑:

select  myString  
--, rtrim(myString)  as newstring -- uncomment for troubleshooting
from #example
WHERE rtrim(myString)  not like '% %'
相关问题