SQL 分割符问题

英文字母打字员 2011-12-12 04:37:55
把一个字段叫做 电话号码:13478908796,12345678,11111 分隔符为,
分离为电话号码1:13478908796 电话号码2:12345678 电话号码3:11111
...全文
89 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2011-12-12
  • 打赏
  • 举报
回复

create table wanglejun(电话号码 varchar(50))

insert into wanglejun select '13478908796,12345678,11111'

-- 拆分为行
select '电话号码'+cast(row_number() over(order by getdate()) as varchar) '电话号码',
substring(a.电话号码,b.number,charindex(',',a.电话号码+',',b.number)-b.number) 'n'
from wanglejun a
inner join master.dbo.spt_values b
on b.[type]='P' and substring(','+a.电话号码,b.number,1)=','

电话号码 n
---------------- -----------------
电话号码1 13478908796
电话号码2 12345678
电话号码3 11111

(3 row(s) affected)


-- 拆分为列
with t as
(select '电话号码'+cast(row_number() over(order by getdate()) as varchar) '电话号码',
substring(a.电话号码,b.number,charindex(',',a.电话号码+',',b.number)-b.number) 'n'
from wanglejun a
inner join master.dbo.spt_values b
on b.[type]='P' and substring(','+a.电话号码,b.number,1)=',')
select [电话号码1], [电话号码2], [电话号码3]
from t
pivot(max(n) for 电话号码 in ([电话号码1], [电话号码2], [电话号码3])) t

电话号码1 电话号码2 电话号码3
------------ ---------- -----------
13478908796 12345678 11111

(1 row(s) affected)
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qianjin036a 的回复:]
SQL code
declare @col varchar(1000)
set @col='13478908796,12345678,11111'
select substring(@col,number,charindex(',',@col+',',number+1)-number) from master..spt_values
where type='p' and number<=l……
[/Quote]
有备份办法没??
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ssp2009 的回复:]
分割?
[/Quote]
对的 把一个字段分割成为3个
-晴天 2011-12-12
  • 打赏
  • 举报
回复
declare @col varchar(1000)
set @col='13478908796,12345678,11111'
select substring(@col,number,charindex(',',@col+',',number+1)-number) from master..spt_values
where type='p' and number<=len(@col) and substring(@col,number,1)<>',' and substring(','+@col,number,1)=','
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13478908796
12345678
11111

(3 行受影响)

*/
快溜 2011-12-12
  • 打赏
  • 举报
回复
分割?
  • 打赏
  • 举报
回复
没人???

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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