Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get substring from varchar column in SQL server table

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.

like image 469
gofor.net Avatar asked Dec 26 '22 00:12

gofor.net


2 Answers

SELECT  SUBSTRING(Name,
        CHARINDEX('(', Name) + 1,
        CHARINDEX(')', Name) - CHARINDEX('(', Name) - 1)
like image 131
Biju P Avatar answered Dec 28 '22 13:12

Biju P


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
like image 45
Devart Avatar answered Dec 28 '22 15:12

Devart