输入: 得到o / p的逻辑:在开括号之前'('将是fname,inside()和@之后将是alias1 andalias2,具体取决于顺序顺序。
Full Name
------------------
ABC (PQR)
(ABC) PQR (XYZ)
ABC @ PQR
ABC @ PQR (XYZ)
ABC (PQR) @123
期望的输出:
FNAME ALIAS1 ALIAS2
-----------------------
ABC PQR
PQR ABC XYZ
ABC PQR
ABC PQR XYZ
ABC PQR 123
答案 0 :(得分:0)
请使用类似的东西 -
;WITH CTE1 AS
(
SELECT REPLACE(REPLACE(REPLACE([Full Name],'(',''),')',''),'@','') [Full Name] FROM Names
)
,CTE AS
(
SELECT [Full Name],
CAST(('<r><n>' + REPLACE([Full Name],' ', '</n><n>') + '</n></r>') AS XML) X
FROM CTE1
)
SELECT
FNAME, CASE WHEN ALIAS1 = '' THEN ALIAS2 ELSE ALIAS1 END ALIAS1,
CASE WHEN ALIAS1 = '' THEN '' ELSE ALIAS2 END ALIAS2
FROM
(
SELECT
i.value('n[1]','VARCHAR(50)') AS FNAME,
ISNULL(i.value('n[2]','VARCHAR(50)'),'') AS ALIAS1,
ISNULL(i.value('n[3]','VARCHAR(50)'),'') AS ALIAS2
FROM CTE c
CROSS APPLY c.X.nodes('/r') x(i)
)z
<强>输出强>
/*------------------------
OUTPUT
------------------------*/
FNAME ALIAS1 ALIAS2
---------------------------- --------- --------------------------------------------------
ABC PQR
ABC PQR XYZ
ABC PQR
ABC PQR
ABC PQR 123
(5 row(s) affected)