SELECT
a.TICKET_ID,
EMP_ID=substring(a.EMP_ID,b.number,charindex(';',a.EMP_ID+';',b.number)-b.number)
FROM T a
join master..spt_values b on b.type='P'
WHERE
charindex(';',';'+a.EMP_ID,b.number)=b.number
select * from t
;with cte as
(select TICKET_ID,CHARINDEX(';',emp_id+';') as station,
SUBSTRING(EMP_ID,1,CHARINDEX(';',emp_id+';')-1) as emp_id from t
union all
select a.ticket_id,
CHARINDEX(';',a.emp_id+';',b.station+1)
,SUBSTRING(a.EMP_ID,b.station+1,CHARINDEX(';',a.emp_id+';',b.station+1)-b.station-1) from t
as a join cte as b on a.TICKET_ID=b.ticket_id
where CHARINDEX(';',a.emp_id+';',b.station+1)>0)
select * from cte
--结果
TICKET_ID EMP_ID
---------- --------------------------------------------------
PO14100001 000001;000002;000003
PO14100002 000001;000004
PO14100003 000003;000004
PO14100004 000001;000002
PO14100005 000001;000002;000003
PO14100006 000001
select * from t
;with cte as
(select TICKET_ID,CHARINDEX(';',emp_id+';') as station,
SUBSTRING(EMP_ID,1,CHARINDEX(';',emp_id+';')-1) as emp_id from t
union all
select a.ticket_id,
CHARINDEX(';',a.emp_id+';',b.station+1)
,SUBSTRING(a.EMP_ID,b.station+1,CHARINDEX(';',a.emp_id+';',b.station+1)-b.station-1) from t
as a join cte as b on a.TICKET_ID=b.ticket_id
where CHARINDEX(';',a.emp_id+';',b.station+1)>0)
select * from cte
--结果
TICKET_ID EMP_ID
---------- --------------------------------------------------
PO14100001 000001;000002;000003
PO14100002 000001;000004
PO14100003 000003;000004
PO14100004 000001;000002
PO14100005 000001;000002;000003
PO14100006 000001
SELECT
a.TICKET_ID,
EMP_ID=substring(a.EMP_ID,b.number,charindex(';',a.EMP_ID+';',b.number)-b.number)
FROM T a
join master..spt_values b on b.type='P'
WHERE
charindex(';',';'+a.EMP_ID,b.number)=b.number
select * from t
;with cte as
(select TICKET_ID,CHARINDEX(';',emp_id+';') as station,
SUBSTRING(EMP_ID,1,CHARINDEX(';',emp_id+';')-1) as emp_id from t
union all
select a.ticket_id,
CHARINDEX(';',a.emp_id+';',b.station+1)
,SUBSTRING(a.EMP_ID,b.station+1,CHARINDEX(';',a.emp_id+';',b.station+1)-b.station-1) from t
as a join cte as b on a.TICKET_ID=b.ticket_id
where CHARINDEX(';',a.emp_id+';',b.station+1)>0)
select * from cte
--结果
TICKET_ID EMP_ID
---------- --------------------------------------------------
PO14100001 000001;000002;000003
PO14100002 000001;000004
PO14100003 000003;000004
PO14100004 000001;000002
PO14100005 000001;000002;000003
PO14100006 000001
select * from t
;with cte as
(select TICKET_ID,CHARINDEX(';',emp_id+';') as station,
SUBSTRING(EMP_ID,1,CHARINDEX(';',emp_id+';')-1) as emp_id from t
union all
select a.ticket_id,
CHARINDEX(';',a.emp_id+';',b.station+1)
,SUBSTRING(a.EMP_ID,b.station+1,CHARINDEX(';',a.emp_id+';',b.station+1)-b.station-1) from t
as a join cte as b on a.TICKET_ID=b.ticket_id
where CHARINDEX(';',a.emp_id+';',b.station+1)>0)
select * from cte
--结果
TICKET_ID EMP_ID
---------- --------------------------------------------------
PO14100001 000001;000002;000003
PO14100002 000001;000004
PO14100003 000003;000004
PO14100004 000001;000002
PO14100005 000001;000002;000003
PO14100006 000001