Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

Please help me out to extract all the data between two particular characters in a column.

For example, I have data in a column data like,

"aaaaa( bb (asdads) bbb )"

And i need the output like,
"aaaaa"

Hint: Need to remove all the data in between "(" & ")" characters

Can anyone help me to get this produce this output in SQL.

What I have tried:

This query is not working properly as i expect,

SQL
SELECT SUBSTRING('aaaaa$bbb$bb$ccccc$ddddd',
CHARINDEX('$','aaaaa$bbbbb$ccccc')+1, 
CHARINDEX('$','aaaaa$bbbbb$ccccc',CHARINDEX('$','aaaaa$bbbbb$ccccc')+1) -
CHARINDEX('$','aaaaa$bbbbb$ccccc')-1) as My_String 
Posted
Updated 28-Apr-19 5:05am
Comments
PIEBALDconsult 18-Jul-17 19:56pm    
SQL is not a good choice for string operations.
I prefer to use a CLR function that uses a Regular Expression.
Graeme_Grant 18-Jul-17 23:02pm    

Below is the query..

DECLARE @String NVARCHAR(max) ='aaaaa( bb (asdads) bbb )'

SELECT LEFT(@String, CHARINDEX('(', @String)-1) +'' +
RIGHT(@String, CHARINDEX(')', REVERSE(@String))-1)
 
Share this answer
 
v2
Comments
Raj 0203 19-Jul-17 8:32am    
This was awesome Ambrish...U saved my Life... Thank You SOOO much
Atlapure Ambrish 19-Jul-17 22:42pm    
Its my pleasure!
Suppose you have name column in table abc and values are-
"aaaaa( bb (asdads) bbb )".

Now I want to split only aaaaa from that table. 
So the query is 

select name,SUBSTRING_INDEX(name,'(',1) AS part1 from abc;
 
Share this answer
 
Comments
Member 13913124 16-Jul-18 6:24am    
n the above solution 2 i want asdads as output, Can anyone suggest that query for me.

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