SQLServer中比较末尾带有空格的字符串

SQLServer中比较字符串的时候 如果字符串末尾是空格 那么SQLServer会无视那些空格直接进行比较。

declare @a nvarchar(50);set @a=N'happycat1988' 
declare @b nvarchar(50);set @b=N'happycat1988 ' 
 
①:“=将忽略末尾空格返回true”
 if(@a = @b
    select 'True' as 直接等号比较
else 
    select 'False' as 直接等号比较
②:"like 将返回false" 
if(@a like @b
    select 'True' as like比较
else 
    select 'False' as like比较
③:“len函数也会忽略末尾空格,但datalength不会”
④:“select 语句中也会忽略空格”
    select * from sysc_user where user_code like '000065' 此语句将查询出user_code 为000065和000065后加入空格的项。
    如果是使用user_code与其他表关联的情况。可能导致延迟加载。
原文摘自https://www.2cto.com/database/201509/441544.html
posted @ 2018-07-03 17:18  majority  阅读(934)  评论(0编辑  收藏  举报