I want to get the string between two strings in a column of a table. How can I select the column? I have written the strings which are on either side. Can anyone help me how to include the column name with the string here?
DECLARE @Text NVARCHAR(MAX)
SELECT
SUBSTRING(@Text, CHARINDEX('Manufacturer Name : ', @Text),
CHARINDEX('Manufacturer Part',@text) - CHARINDEX('Manufacturer Name : ', @Text) + LEN('Manufacturer Part'))
FROM
tbIMPACArchiveNew
WHERE
(Description LIKE '%Manufacturer Name: %'
OR Description LIKE '%Manufacturer Name : %')
AND Description LIKE '%Manufacturer Part%'
Expected result:
Column A Expected result
Manufacturer Name : ABC Manufacturer Part Number : XVB-C2B4 ABC
Manufacturer Name : DEF Manufacturer Part Number : 3RH1924-1GP11 DEF
Manufacturer Name : ABJ Manufacturer Part Number : FLDP-IOM248-0001 ABJ
Manufacturer Name : HIJ Manufacturer Part Number : L12/5MLLD0035 HIJ
Manufacturer Name : abhkdk Manufacturer Part Number : PEH1083510 abhkdk
Manufacturer Name : 1245 PUMP Manufacturer Part Number : 02-1010-55 1245
Here is a way to extract the text between two fixed strings. Not exactly sure what you were doing with the @Text variable so I used it as an example below.
For a sample data of below
you can use
If you notice I am removing 2 characters at the end of substring to eliminate 'M' from
Manufacturer Part
and the Space before the actual text. You can useRTRIM
but i chose this way.You can get the index of 4th space and remove the first 20.
I think this what you want
where
20
is the length of "Manufacturer Name : "