I need to extract from a string. Given below are the IDs that are available.
0234-RDRT-RS111-M-EU
0234-RDRT-RSD123-M-EU
I need to extract the data after second "-" and only first 5 characters.
Pls help me with the query.
I need to extract from a string. Given below are the IDs that are available.
0234-RDRT-RS111-M-EU
0234-RDRT-RSD123-M-EU
I need to extract the data after second "-" and only first 5 characters.
Pls help me with the query.
SUBSTRING(string, CHARINDEX('-', string + '-', CHARINDEX('-', string) + 1) + 1, 5)
Examples:
SELECT string, SUBSTRING(string, CHARINDEX('-', string + '-', CHARINDEX('-', string) + 1) + 1, 5) AS substring
FROM ( VALUES('0234-RDRT-RS111-M-EU'),('0234-RDRT-RSD123-M-EU'),('ABC') ) data(string)
thankyou.
DECLARE @t table(id int IDENTITY, stringVal varchar(200));
INSERT @t (stringVal)
VALUES ('0234-RDRT-RS111-M-EU'),('0234-RDRT-RSD123-M-EU');
SELECT
t2.stringVal
, Stuff((SELECT '-' + d.Item
FROM @t t
CROSS APPLY [dbo].[DelimitedSplit8K](t.stringVal, '-') d
WHERE ItemNumber>2
AND t.id=t2.id
FOR XML PATH(''), TYPE)
.value('text()[1]','nvarchar(max)'),1,1,N'') stringValMinusFirst2Parts
FROM @t t2;
@jotorre:
Did you read the requirements?: