现有数据,比如爱好hobby,有多个爱好用逗号隔开放在一起存储,现在需要把他们分开

最终结果

直接上脚本
use DB01; -- 建表 create table dbo.tb_hobby( id int null ,name varchar(10) null
,hobby varchar(100) null ); -- 插入数据 insert into dbo.tb_hobby values
('1001','朱梅拉','跑步,踢足球,打篮球'); insert into dbo.tb_hobby values
('1002','李格策','书法,跑步'); -- 查询 select * from dbo.tb_hobby; -- 拆分成行 select a.id
,a.name ,SUBSTRING(a.hobby,number,CHARINDEX(',',a.hobby+',',number)-number) as
hobby from tb_hobby a,master..spt_values where number >= 1 and number <
len(a.hobby) and type='p' and SUBSTRING(','+a.hobby,number,1)=',' ;
 

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信