right in sql 不适用于 where 子句

right in sql is not working on the where clause

当我有这个where条款

WHERE MT.Media = 'Telephone' AND
LEN('0557500007')>8 and [MediaValue]='0557500007' 

我在sql管理工作室排了一排

但是当我做这个的时候where

WHERE MT.Media = 'Telephone' AND
LEN('0557500007')>8 and RIGHT([MediaValue],8) =RIGHT('0557500007',8)

我得到的结果是空的,为什么?

从第一个 where 子句中,您可以看到我这样做了这个值 0557500007,但是为什么当我应用第二个 where 子句时它没有显示?

WHERE MT.Media = 'Telephone' AND
LEN('0557500007')>8 and RIGHT(rtrim([MediaValue]),8) =RIGHT('0557500007',8)

SQL-服务器用空格填充一个字符串以便与另一个字符串进行比较。他们声称此行为符合标准(至少 SQL-92):https://support.microsoft.com/kb/316626/en-us .

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.

因此与其他 DBMS 不同,SQL-Server 将“0557500007”和“0557500007”视为相等。因此,您的列值似乎有尾随空格。使用 RTRIM 删除这些:

right(rtrim(MediaValue]),8)