if object_id('tb') is not null drop table tb
create table tb(id int identity(1,1),s nvarchar(100))
insert into tb(s) select '车位地址1,车位状况1|车位地址2,车位状况2|车位地址n,车位状况n'
;with cte as(
select substring(s,number,charindex('|',s+'|',number)-number) as ss
from tb with(nolock),master..spt_values with(nolock)
where type='P' and number>=1 and number<=len(s)
and substring('|'+s,number,1)='|'
)select left(ss,charindex(',',ss)-1)as s1,substring(ss,charindex(',',ss)+1,len(ss))as s2 from cte
drop table tb
/**
s1 s2
----------- ------------
车位地址1 车位状况1
车位地址2 车位状况2
车位地址n 车位状况n
*/
或者
if object_id('tb') is not null drop table tb
go
create table tb([编号] varchar(3),[产品] varchar(2),[数量] int,[单价] int,[金额] int,[序列号] varchar(8))
insert into tb([编号],[产品],[数量],[单价],[金额],[序列号])
select '001','AA',3,5,15,'12,13,14' union all
select '002','BB',8,9,13,'22,23,24'
go
select [编号],[产品],[数量],[单价],[金额]
,substring([序列号],b.number,charindex(',',[序列号]+',',b.number)-b.number) as [序列号]
from tb a with(nolock),master..spt_values b with(nolock)
where b.number>=1 and b.number<len(a.[序列号]) and b.type='P'
and substring(','+[序列号],number,1)=','
go
drop table tb
go
/**
编号 产品 数量 单价 金额 序列号
---- ---- ----------- ----------- ----------- --------
AA 3 5 15 12
AA 3 5 15 13
AA 3 5 15 14
BB 8 9 13 22
BB 8 9 13 23
BB 8 9 13 24
*/
----------