Get substring from varchar column in SQL server table

sql, sql-server, sql-server-2005

Solution

Try this one -

DECLARE @temp TABLE (st NVARCHAR(50))

INSERT INTO @temp (st)
VALUES 
     ('001 BASI Distributor (EXAM)'),
     ('002 BASI Supplier (EXAM2)'),
     ('MASI DISTRIBUTOR (EXAM002)'),
     ('MASI SUPPLIER (EXAM003)'),
     ('EXAM_ND Distributor Success System Test (EXAM_ND)'),
     ('EXAM_SS Supplier Success System Test (EXAM_SS)')

SELECT SUBSTRING(
     st, 
     CHARINDEX('(', st) + 1, 
     CHARINDEX(')', st) - CHARINDEX('(', st) - 1
)
FROM @temp

Output -

-------------
EXAM
EXAM2
EXAM002
EXAM003
EXAM_ND
EXAM_SS

Problem

I have one column called Name and in the column I have values ``` Name 001 BASI Distributor (EXAM) 002 BASI Supplier (EXAM2) MASI DISTRIBUTOR (EXAM002) MASI SUPPLIER (EXAM003) EXAM_ND Distributor Success System Test (EXAM_ND) EXAM_SS Supplier Success System Test (EXAM_SS) ``` now I want to separate the value inside the `()` from this whole string.How I will get this I tried for the `SUBSTRING (Name ,22 ,4 )` but this will help for single one I want to get the result using some unique solution.

Original source