How to select a string between two strings in a co

2020-04-17 04:30发布

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

4条回答
走好不送
2楼-- · 2020-04-17 05:10

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'
查看更多
劫难
3楼-- · 2020-04-17 05:11

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.

查看更多
看我几分像从前
4楼-- · 2020-04-17 05:15

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

SELECT SUBSTRING ( Description , 20 , instr(Description,' ',1,4 ) - 20)
FROM  tbIMPACArchiveNew;
查看更多
迷人小祖宗
5楼-- · 2020-04-17 05:17

I think this what you want

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

where 20 is the length of "Manufacturer Name : "

查看更多
登录 后发表回答