Aviadjo
Aviadjo

Reputation: 655

How to take substring from field between specific chars

I have field which contain a value like:

F28_TIT245_0_90A_OOC_LCL by AARBITMA(aarbitma@LC13UPE-CIM)

I want to take the name between '(' and '@' which is "aarbitma".

How can I do it?

I'm running on SQL Server 2008R2

Upvotes: 2

Views: 2626

Answers (1)

sll
sll

Reputation: 62564

-- there are no checks for wrong input text
-- this is TODO to be done yourself
DECLARE @text VARCHAR(100)
DECLARE @start INT
DECLARE @end INT
SET @text = 'F28_TIT245_0_90A_OOC_LCL by AARBITMA(aarbitma@LC13UPE-CIM)'

SELECT @start = PATINDEX('%(%@%', @text)
SELECT @end = CHARINDEX('@', @text, 0)
SELECT SUBSTRING(@text, @start+1, @end-@start-1)

MSDN:

Upvotes: 4

Related Questions