我的表格中有一个 varchar
列,其中可以包含不同格式的电话号码以及其中的一些文本。
示例:
"This is a test 111-222-3344"
"Some Sample Text (111)-222-3344"
"Hello there 1112223344 . How are you?"
如何从中提取电话号码?我查找了其他解决方案( Another Post ),但它们不符合我的要求。
谢谢
最佳答案
好吧,由于它们的格式不同,我会以相同的格式提取它们。
--Handles parentheses, commas, spaces, hyphens..
declare @table table (c varchar(256))
insert into @table
values
('This is a test 111-222-3344'),
('Some Sample Text (111)-222-3344'),
('Hello there 111222 3344 / How are you?'),
('Hello there 111 222 3344 ? How are you?'),
('Hello there 111 222 3344. How are you?')
select
replace(LEFT(SUBSTRING(replace(replace(replace(replace(replace(c,'(',''),')',''),'-',''),' ',''),',',''), PATINDEX('%[0-9.-]%', replace(replace(replace(replace(replace(c,'(',''),')',''),'-',''),' ',''),',','')), 8000),
PATINDEX('%[^0-9.-]%', SUBSTRING(replace(replace(replace(replace(replace(c,'(',''),')',''),'-',''),' ',''),',',''), PATINDEX('%[0-9.-]%', replace(replace(replace(replace(replace(c,'(',''),')',''),'-',''),' ',''),',','')), 8000) + 'X') -1),'.','')
from @table
关于sql - 如何从 SQL Server 中的 varchar 列中提取电话号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355290/