Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
How do i get all the strings in CSV format which are enclosed by $$$ symbols in sql server.

IF i have a string as

Dear $$$Name$$$ your organization is $$$Organization$$$
my output needs to be like
(Name,Organization)

there can be any number of $$$ enclosed strings in my input string and this solution is to be in SQL.

Please advice. Thanx
Posted
Updated 9-Jun-15 22:10pm
v2
Comments
Tomas Takac 10-Jun-15 2:38am    
What did you try? Where are you stuck?
Vinay1337 10-Jun-15 2:40am    
I am not even getting how to start
The idea i thought was to iterate until the string length goes zero and get the first two appearances of $$$ and get the sub string between them but don't know how to achieve that.
any algo or idea is welcome.
Maciej Los 10-Jun-15 2:54am    
I don't get you...
Do you want to get text between $$$?
Vinay1337 10-Jun-15 2:58am    
yes i want the text between two pairs of $$$ in the given input string
if the string is Dear $$$name$$$ then i want 'name' as the output

As to analogy in solution 3, below solution provides a way to get values between a pair of $$$. Now you have to insert words into temporary table to be able to merge it into single comma separated column.

SQL
;WITH SignPairs AS
(
    SELECT @myText AS InputText, CHARINDEX('$$$', @myText) AS Pair1,
            CHARINDEX('$$$', @myText, CHARINDEX('$$$', @myText)+3) AS Pair2, 1 AS Occurence
    WHERE CHARINDEX('$$$', @myText)>0 AND CHARINDEX('$$$', @myText, CHARINDEX('$$$', @myText)+3)>0
    UNION ALL
    SELECT InputText AS InputText, CHARINDEX('$$$', InputText, Pair2+3) AS Pair1,
            CHARINDEX('$$$', InputText, CHARINDEX('$$$', InputText, Pair2+3)+3) AS Pair2, Occurence + 1 AS Occurence
    FROM SignPairs
    WHERE CHARINDEX('$$$', InputText, Pair2+3)>0 AND CHARINDEX('$$$', InputText, CHARINDEX('$$$', InputText, Pair2+3)+3)>0
)
SELECT InputText, Pair1, Pair2, Occurence, REPLACE(SUBSTRING(InputText,Pair1, Pair2 - Pair1), '$$$', '') AS Word
FROM SignPairs


Result:
InputText														Pair1	Pair2	Occurence	Word
Dear $$$First Name$$$ your organization is $$$Organization$$$	6		19		1		First Name
Dear $$$First Name$$$ your organization is $$$Organization$$$	44		59		2		Organization
 
Share this answer
 
Comments
Vinay1337 10-Jun-15 5:01am    
Thank a lot man
Maciej Los 10-Jun-15 5:10am    
You're very welcome ;)
Vinay1337 3-Aug-15 6:39am    
Hey man need your help on the following question if you can

http://www.codeproject.com/Questions/1015379/Parse-JSON-in-sql-server?arn=0
Check out these String Functions (Transact-SQL)[^] to construct your sql statement.
Specifically, I think you will need substring, len, and charindex.
 
Share this answer
 
Set a variable Initial Position = to 0
Create a while loop
find the first position of your token $$$ using SubString
set the start position value
find the next instance of the token
set the end position
grab the text between start and end position
set the initial position to the end position

repeat until the end of the text!
 
Share this answer
 
Try this:
SQL
DECLARE @myText VARCHAR(255) = 'Dear $$$Name$$$ your organization is $$$Organization$$$'

DECLARE @tmp TABLE (InputText VARCHAR(255), Word VARCHAR(255))
--split text to words and insert into new table (table variable)
;WITH CTE AS
(
	SELECT @myText AS InputText, LEFT(@myText, CHARINDEX(' ', @myText) - 1) AS Word, RIGHT(@myText, LEN(@myText) - CHARINDEX(' ', @myText)) AS Remainder
	WHERE CHARINDEX(' ', @myText)>0
	UNION ALL
	SELECT InputText AS InputText, LEFT(Remainder, CHARINDEX(' ', Remainder) - 1) AS Word, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(' ', Remainder)) AS Remainder
	FROM CTE
	WHERE CHARINDEX(' ', Remainder)>0
	UNION ALL
	SELECT InputText AS InputText, Remainder AS Word, NULL AS Remainder
	FROM CTE
	WHERE CHARINDEX(' ', Remainder)=0
)
INSERT INTO @tmp (InputText, Word)
SELECT InputText, REPLACE(Word, '$$$', '') AS Word
FROM CTE
WHERE PATINDEX('$$$%$$$', Word)>0

--get comma separated text
SELECT DISTINCT InputText, STUFF((SELECT ', ' + t2.Word 
        FROM @tmp AS t2
		WHERE t1.InputText = t2.InputText
        FOR XML PATH('')), 1, 2, '') AS Cols
FROM @tmp t1


Result:
InputText	                                            Cols
Dear $$$Name$$$ your organization is $$$Organization$$$	Name, Organization
 
Share this answer
 
Comments
Vinay1337 10-Jun-15 4:16am    
This works fine but fails when there are spaces in text between $$$ such as
Dear $$$First Name$$$ it fails to get this
Maciej Los 10-Jun-15 4:49am    
See solution5 ;)
Adapted from this link[^] - adaption was to cater for longer separator

SQL
-- Adapted from http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/

DECLARE @string VARCHAR(MAX) = 'Dear $$$Name$$$ your organization is $$$Organization$$$'
DECLARE @delimiter VARCHAR(3) = '$$$'

DECLARE @output TABLE (splitdata NVARCHAR(MAX))

DECLARE @start INT, @end INT 
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
WHILE @start < LEN(@string) + 1 BEGIN 
    IF @end = 0  
        SET @end = LEN(@string) + LEN(@delimiter)
       
    INSERT INTO @output (splitdata)  
    VALUES(SUBSTRING(@string, @start, @end - @start)) 
    SET @start = @end + LEN(@delimiter)
    SET @end = CHARINDEX(@delimiter, @string, @start)
        
END 
SELECT '!' + splitdata + '!' FROM @output
Results ('!' added to highlight that spaces are included!)
Dear !
!Name!
! your organization is !
!Organization!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900