【问题标题】:SQL: extracting a number in middle of a cell from a different tableSQL:从不同的表中提取单元格中间的数字
【发布时间】:2019-05-27 16:58:00
【问题描述】:

我需要帮助从另一个表中提取一个数字中的一个数字。 我会解释: 第一张桌子有电话号码。 例如:+12125634533, +41542858585

第二张桌子有

country code | second column
-------------+--------------
1            | usa
41           | switzerland

如何在数字中获取运算符?

示例:

+12125634533 -- 运营商是212(1 是国家代码,5634533 是电话号码 - 总是 7 个数字)

+41542858585 -- 运营商是54(41是国家代码,2858585是电话号码)。

【问题讨论】:

  • 最后总是有 7 位数字吗?
  • 我正在使用在 sql-server 上运行的 SSMS
  • 是的,最后总是有 7 个数字。

标签: sql sql-server ssms


【解决方案1】:

假设电话前缀是唯一的,并且电话号码总是以+ 开头,那么是这样的:

select t1.*,
       substring(t1.phonenumber, 2 + len(t2.countrycode),
                 len(t1.phonenumber) - 7 - len(t2.countrycode) - 1
                )
from table1 t1 left join
     table2 t2
     on t1.phonenumber like '+' + t2.countrycode + '%';

Here 是一个 dbfiddle。

【讨论】:

  • 关闭。一开始我还有1个额外的号码。 1(用于1212)和1(在第二个141上)
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 2011-08-28
  • 1970-01-01
相关资源
最近更新 更多