@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

最佳答案

如果只想比较最右边的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)

如果只有一个表的supplierinvoicenumbers超过9个字符,则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

10-08 01:16