Split Field values

Joseph s 1 Reputation point

Hi all, I need a little help to extract all the string within and including the brackets and output them into separate columns and into a final table please. Here is an example of some of the fields in the source table.

 CREATE TABLE TableName   

     (FieldName nvarchar(max))   


 INSERT INTO dbo.TableName   


 SELECT 'Urology Consultants OSSD (GSD)' UNION   

 SELECT 'Speech and Lang Speech Recdd (BSDF)' UNION   

 SELECT 'Psychology Con Speech Recdd (FD)' UNION   

 SELECT 'Immunology/Allergy Consultant (HRB)' UNION   

 SELECT 'Gastro Endoscopy Nurseses (HRB)' UNION   

 SELECT 'Histology-Cellular Pathology Speech Reced (WFSP)'     


 SELECT *    

 FROM dbo.TableName   

I would like the output to be like the below.



SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
8,500 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Tom Phillips 17,511 Reputation points

    How is this different from your other question? The solution is the same.


  2. MelissaMa-MSFT 24,116 Reputation points

    Hi @Joseph s

    Welcome to Microsoft Q&A!

    Please refer your previous post mentioned by other experts which is similar with this post. You could only replace '[]' with '()' in the queries.

    Please also refer below:

    SELECT *,LEFT(FieldName,CHARINDEX('(',FieldName)-2) COL1,  
    SUBSTRING(FieldName,CHARINDEX('(',FieldName)+1,LEN(FieldName)-CHARINDEX('(',FieldName)-1) COL2  
    FROM dbo.TableName   

    If '(' is missing in some rows, you could refer below:

    SELECT *,CASE WHEN CHARINDEX('(',FieldName)<>0 THEN LEFT(FieldName,CHARINDEX('(',FieldName)-1) ELSE FieldName END COL1,  
    CASE WHEN CHARINDEX('(',FieldName)<>0 THEN SUBSTRING(FieldName,CHARINDEX('(',FieldName)+1,CHARINDEX(')',FieldName)-CHARINDEX('(',FieldName)-1) ELSE '' END COL2  
    FROM dbo.TableName   


    If above is still not working, please provide more sample data and expected output.

    Thanks for your undertstanding.

    Best regards

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments