I have a query
declare @strString varchar(500)
set @strString ='Terminal$Attr1,Attr2,Attr3,Attr4,Attr5,Attr6,@Connector$Con1,Con2,Con3,Con4,@Wire$W1,W2,W3,W4,W5,'
;WITH StrCTE(start, stop) AS
(
SELECT 1, CHARINDEX('$' , @strString )
UNION ALL
SELECT stop + 1, CHARINDEX(',' ,@strString , stop + 1)
FROM StrCTE
WHERE stop > 0
)
SELECT SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS stringValue
FROM StrCTE
where SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END)<>''
which is providing me the output :
stringValue
Terminal
Attr1
Attr2
Attr3
Attr4
Attr5
Attr6
@Connector$Con1
Con2
Con3
Con4
@Wire$W1
W2
W3
W4
W5
but I need the output as :
stringValue
Terminal
Attr1
Attr2
Attr3
Attr4
Attr5
Attr6
Connector
Con1
Con2
Con3
Con4
Wire
W1
W2
W3
W4
W5
使用替换( < > , '@' , '')
SELECT REPLACE(SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END, '@', '') AS stringValue
Try this made changes in CTE
declare @strString varchar(500)
set @strString ='Terminal$Attr1,Attr2,Attr3,Attr4,Attr5,Attr6,@Connector$Con1,Con2,Con3,Con4,@Wire$W1,W2,W3,W4,W5,'
Set @strString=REPLACE(REPLACE(@strString,'$',','),'@','')--Added
;WITH StrCTE(start, stop) AS
(
SELECT 1, CHARINDEX(',' , @strString )
UNION ALL
SELECT stop + 1, CHARINDEX(',' ,@strString , stop + 1)
FROM StrCTE
WHERE stop > 0
)
SELECT SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS stringValue
FROM StrCTE
where SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END)<>''
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.