-3

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  
1
  • 1
    Seems to me that the true requirement is to remove the '@' token and to split on the '$' token, if this shows all the rules.
    – Gimby
    Mar 11, 2016 at 7:59

2 Answers 2

0

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)<>''
0

use replace( < > , '@' , '')

SELECT   REPLACE(SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END, '@', '') AS stringValue

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.