简体   繁体   中英

How to select a string between two strings in a column in SQL Server?

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

For a sample data of below

declare @table table (id int identity(1,1), data varchar(1000), descr varchar(1000))

insert into @table values ('Manufacturer Name : Manufacturer 1 Manufacturer Part : asjdfj','First Manufacturer')
insert into @table values ('Manufacturer Name : Manufacturer 2 Manufacturer Part : asjsadfasdfdfj','Second Manufacturer')
insert into @table values ('Manufacturer Name : Manufacturer 3 Manufacturer Part : er6ty','Third Manufacturer')

you can use

select substring(data, 
                 charindex('Manufacturer Name : ', data) + len('Manufacturer Name : '),
                 charindex('Manufacturer Part : ',data) - len('Manufacturer Part :') - 2) as Manufacturer_Name
       , descr
from @table

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 use RTRIM but i chose this way.

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.

DECLARE @Text NVARCHAR(MAX) = 'blah blah Manufacturer Name : MY NAME Manufacturer Part blah blah'
SELECT SUBSTRING(@Text, CHARINDEX('Manufacturer Name : ', @Text) + LEN('Manufacturer Name : ') + 1, CHARINDEX('Manufacturer Part',@Text) - (CHARINDEX('Manufacturer Name : ', @Text) + 2 + LEN('Manufacturer Name : ')) )

SELECT SUBSTRING(Description, CHARINDEX('Manufacturer Name : ', Description) + LEN('Manufacturer Name : ') + 1, CHARINDEX('Manufacturer Part',Description) - (CHARINDEX('Manufacturer Name : ', Description) + 2 + LEN('Manufacturer Name : ')) )
FROM tbIMPACArchiveNew 
WHERE Description LIKE '%Manufacturer Name : %Manufacturer Part'

I think this what you want

SELECT SUBSTRING ( Description , 20 , CHARINDEX("Manufacturer Part") - 20)
FROM  tbIMPACArchiveNew;

where 20 is the length of "Manufacturer Name : "

You can get the index of 4th space and remove the first 20.

SELECT SUBSTRING ( Description , 20 , instr(Description,' ',1,4 ) - 20)
FROM  tbIMPACArchiveNew;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM