如何在SQL Server中比较两个相同的值

时间:2016-12-08 12:25:25

标签: sql-server database

@Var1=CHA000375972
@Var2=CHA000375972
@Var2=000375972

此处@Var1@Var2完全相同,但@Var3@Var1@Var2相同。那么我们如何比较两个值是否相同呢?

我的目标是获得所有值是完全相同还是几乎相同

select distinct a.SupplierInvoiceNumber
from #tmpSup1 a  
join #tmpSup2 b on a.SupplierInvoiceNumber = b.SupplierInvoiceNumber 

1 个答案:

答案 0 :(得分:1)

如果您只想比较最右边的9个字符,可以使用:

right(@Var1,9) = right(@Var3,9)

select distinct a.SupplierInvoiceNumber
  from #tmpSup1 a 
    join #tmpSup2 b on right(a.SupplierInvoiceNumber,9)=right(b.SupplierInvoiceNumber,9)

如果只有一个表具有长度超过9个字符的SupplierInvoiceNumbers需要在right()函数中,则连接条件可以简化为:

    join #tmpSup2 b on right(a.SupplierInvoiceNumber,9)=b.SupplierInvoiceNumber

    join #tmpSup2 b on a.SupplierInvoiceNumber=right(b.SupplierInvoiceNumber)

如果一个变量总是比另一个变量短,但不总是9个字符,则可以使用:

select distinct a.SupplierInvoiceNumber
  from #tmpSup1 a 
    join #tmpSup2 b on a.SupplierInvoiceNumber like '%'+b.SupplierInvoiceNumber