高分求sql语句

qiujg 2007-12-04 03:11:17
SELECT 电话 FROM table1
phone为电话号码,值为格式:86-010-12345678,如何将其拆分为三列显示

比如:
电话
86-010-12345678
86-020-87654321

拆分后显示
国家代码 地区代码 电话号码
86 010 12345678
86 020 87654321
...全文
214 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zghua851004 2007-12-04
  • 打赏
  • 举报
回复
只能看懂晓风的代码,小虫的也能看懂,潇洒乌龟的代码看不懂,哎。。。努力ING
-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
接分的
dawugui 2007-12-04
  • 打赏
  • 举报
回复
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 行)
*/
kk19840210 2007-12-04
  • 打赏
  • 举报
回复


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
dawugui 2007-12-04
  • 打赏
  • 举报
回复
最好的办法是飞天小虫的。他已经写了,我就不写了.
hgs5945 2007-12-04
  • 打赏
  • 举报
回复
declare @code varchar(50)
set @code = '852-123456'
select substring(@code,1,3) as '国家',
substring(@code,0,0) as '地方' ,
substring(@code,5,6) as '电话'
hgs5945 2007-12-04
  • 打赏
  • 举报
回复
"潇洒老乌龟"的方法和"(晓)风残月"的方法都可以实现
不过好像"(晓)风残月"的方法比较好理解一点哦,你就看着使用吧
qiujg 2007-12-04
  • 打赏
  • 举报
回复
谢谢各位:如果电话为:852-123456假设这是香港的
应该显示
国家代码 地区代码 电话号码
852 123456
上面的语句都实现不了哦
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
能接点分吗
dawugui 2007-12-04
  • 打赏
  • 举报
回复
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 行)
*/
火星求索 2007-12-04
  • 打赏
  • 举报
回复
固定长度

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 '电话'
dawugui 2007-12-04
  • 打赏
  • 举报
回复
select 国家代码 = left(电话 , charindex('-',电话) - 1) , 
地区代码 = substring(电话,charindex('-',电话) + 1 , charindex('-',电话,charindex('-',电话) + 1) - charindex('-',电话) - 1) ,
电话号码 = substring(电话,charindex('-',电话,charindex('-',电话) + 1) ,len(电话))
from tb
fcuandy 2007-12-04
  • 打赏
  • 举报
回复
jf
kk19840210 2007-12-04
  • 打赏
  • 举报
回复


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
kk19840210 2007-12-04
  • 打赏
  • 举报
回复


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
kk19840210 2007-12-04
  • 打赏
  • 举报
回复


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
kk19840210 2007-12-04
  • 打赏
  • 举报
回复


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

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧