34,838
社区成员




create table tb (电话 varchar(30))
insert into tb values('86-010-12345678')
insert into tb values('86-020-87654321')
insert into tb values('852-123456')
go
select 国家代码 = left(电话 , charindex('-',电话) - 1) ,
地区代码 = substring(电话,charindex('-',电话) + 1 , charindex('-',电话,charindex('-',电话) + 1) - charindex('-',电话) - 1) ,
电话号码 = substring(电话,charindex('-',电话,charindex('-',电话) + 1) + 1 ,len(电话))
from tb
where charindex('-',电话) > 0 and charindex('-',电话,charindex('-',电话) + 1) > 0
union all
select 国家代码 = left(电话 , charindex('-',电话) - 1) ,
地区代码 = '',
电话号码 = substring(电话,charindex('-',电话) + 1 ,len(电话))
from tb
where charindex('-',电话) > 0 and charindex('-',电话,charindex('-',电话) + 1) <= 0
drop table tb
/*
国家代码 地区代码 电话号码
------------------------------ ------------------------------ ------------------------------
86 010 12345678
86 020 87654321
852 123456
(所影响的行数为 3 行)
*/
select 国家代码 =case when PARSENAME(replace(phone,'-','.'),3) is null then PARSENAME(replace(phone,'-','.'),2) else PARSENAME(replace(phone,'-','.'),3) end
, 地区代码= case when PARSENAME(replace(phone,'-','.'),3) is null then '' else PARSENAME(replace(phone,'-','.'),2) end,电话号码 =PARSENAME(replace(phone,'-','.'),1) from table1
create table tb (电话 varchar(30))
insert into tb values('86-010-12345678')
insert into tb values('86-020-87654321')
go
select 国家代码 = left(电话 , charindex('-',电话) - 1) ,
地区代码 = substring(电话,charindex('-',电话) + 1 , charindex('-',电话,charindex('-',电话) + 1) - charindex('-',电话) - 1) ,
电话号码 = substring(电话,charindex('-',电话,charindex('-',电话) + 1) + 1 ,len(电话))
from tb
where charindex('-',电话) > 0 and charindex('-',电话,charindex('-',电话) + 1) > 0
drop table tb
/*
国家代码 地区代码 电话号码
------------------------------ ------------------------------ ------------------------------
86 010 12345678
86 020 87654321
(所影响的行数为 2 行)
*/
declare @code varchar(50)
set @code = '86-010-12345678'
select substring(@code,1,2) as '国家',
substring(@code,4,3) as '地方' ,
substring(@code,8,8) as '电话'
select 国家代码 = left(电话 , charindex('-',电话) - 1) ,
地区代码 = substring(电话,charindex('-',电话) + 1 , charindex('-',电话,charindex('-',电话) + 1) - charindex('-',电话) - 1) ,
电话号码 = substring(电话,charindex('-',电话,charindex('-',电话) + 1) ,len(电话))
from tb
create table table1 (phone varchar(100))
insert into table1 values('86-010-12345678')
insert into table1 values('86-020-87654321')
select 国家代码 =PARSENAME(replace(phone,'-','.'),3), 地区代码=PARSENAME(replace(phone,'-','.'),2),电话号码 =PARSENAME(replace(phone,'-','.'),1) from table1
国家代码 地区代码 电话号码
86 010 12345678
86 020 87654321
create table table1 (phone varchar(100))
insert into table1 values('86-010-12345678')
insert into table1 values('86-020-87654321')
select 国家代码 =PARSENAME(replace(phone,'-','.'),3), 地区代码=PARSENAME(replace(phone,'-','.'),2),电话号码 =PARSENAME(replace(phone,'-','.'),1) from table1
国家代码 地区代码 电话号码
86 010 12345678
86 020 87654321
create table table1 (phone varchar(100))
insert into table1 values('86-010-12345678')
insert into table1 values('86-020-87654321')
select 国家代码 =PARSENAME(replace(phone,'-','.'),3), 地区代码=PARSENAME(replace(phone,'-','.'),2),电话号码 =PARSENAME(replace(phone,'-','.'),1) from table1
国家代码 地区代码 电话号码
86 010 12345678
86 020 87654321
create table table1 (phone varchar(100))
insert into table1 values('86-010-12345678')
insert into table1 values('86-020-87654321')
select 国家代码 =PARSENAME(replace(phone,'-','.'),3), 地区代码=PARSENAME(replace(phone,'-','.'),2),电话号码 =PARSENAME(replace(phone,'-','.'),1) from table1
国家代码 地区代码 电话号码
86 010 12345678
86 020 87654321