像这样的?
CREATE TABLE table1 (diagnosis varchar(100), diagnosis_1 varchar(10), diagnosis_2 varchar(10), diagnosis_3 varchar(10));
INSERT INTO table1 (diagnosis)
VALUES
('T038MFRACTURE'),
('M719BOCHCM531'),
('F900CF334M75');
WITH PosOfNonNumer AS
(
SELECT diagnosis AS Original
,diagnosis
,1 AS PartIndex
,PATINDEX('%[A-Z][0-9]%',diagnosis) AS PosFound
,SUBSTRING(diagnosis,1,5) AS PartFound
,SUBSTRING(diagnosis,PATINDEX('%[A-Z][0-9]%',diagnosis)+2,1000) AS RestString
FROM table1
UNION ALL
SELECT p.Original
,p.RestString
,p.PartIndex+1
,PATINDEX('%[A-Z][0-9]%',p.RestString) AS PosFound
,SUBSTRING(p.RestString,PATINDEX('%[A-Z][0-9]%',p.RestString),5) AS PartFound
,SUBSTRING(p.RestString,PATINDEX('%[A-Z][0-9]%', p.RestString)+2,1000) AS RestString
FROM PosOfNonNumer AS p
WHERE PATINDEX('%[A-Z][0-9]%',p.RestString)>0
)
--主查询使用条件聚合来透视结果
SELECT Original
,MAX(CASE WHEN PartIndex=1 THEN PartFound END) AS diag1
,MAX(CASE WHEN PartIndex=2 THEN PartFound END) AS diag2
,MAX(CASE WHEN PartIndex=3 THEN PartFound END) AS diag3
,MAX(CASE WHEN PartIndex=4 THEN PartFound END) AS diag4
FROM PosOfNonNumer
GROUP BY Original
GO
--clean-up
--DROP TABLE table1;
Original diag1 diag2 diag3 diag4
F900CF334M75 F900C F334M M75 NULL
M719BOCHCM531 M719B M531 NULL NULL
T038MFRACTURE T038M NULL NULL NULL
最后你必须删掉一些角色。。。希望你能自己解决。。。