标签: sql sql-server
我有两张带有电话号码的表格,这些号码可能有也可能没有+前缀。我必须神奇地加入这两张桌子!
如果我想避免使用嵌套的select子句在必要时将数据与+连接,我有哪些选择?
答案 0 :(得分:2)
你可以采取两种方式
REPLACE REPLACE(phone,'+','')
REPLACE
REPLACE(phone,'+','')
如果数字长度不变 - SUBSTRING SUBSTRING(phone, (-1)*@length) @length中您的号码长度
SUBSTRING
SUBSTRING(phone, (-1)*@length)
@length
所以,联接看起来像是 A JOIN B ON REPLACE(A.phone,'+','') = REPLACE(B.phone,'+','') 或 A JOIN B ON SUBSTRING(A.phone, @length) = SUBSTRING(B.phone, @length)
A JOIN B ON REPLACE(A.phone,'+','') = REPLACE(B.phone,'+','')
A JOIN B ON SUBSTRING(A.phone, @length) = SUBSTRING(B.phone, @length)