简体   繁体   English

从字符串中提取所需的子字符串

[英]Extract desired substring from a string

I want to remove all the raw data before and after the specific tag.我想删除特定标签前后的所有原始数据。 eg I have data in a variable like例如,我有一个变量中的数据,例如

declare @getdata varchar(max)= '
--uuid:674de154-e967-4153-84a4-549525203f52
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org
<LicensingReportProcessResult 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.acord.org/schema/data/draft/ReusableDataComponents/1" 
</LicensingReportProcessResult>
--uuid:674de154-e967-4153-84a4-549525203f52--'

I want to remove all raw data before <LicensingReportProcessResult tag and after tag I want result like this:我想删除 <LicensingReportProcessResult 标记之前和标记之后的所有原始数据,我想要这样的结果:

@getdata =
'<LicensingReportProcessResult 
Only the data between tag 
</LicensingReportProcessResult>'

I have done so far到目前为止我已经做了

select @getdata = right(@getdata, len(@getdata) - 
    charindex('<LicensingReportProcessResult', @getdata))

select @getdata = CONCAT('<',@getdata) 

select @getdata = left(@getdata, len(@getdata) - 
    charindex('</LicensingReportProcessResult>', @getdata))

Some fairly straight forward string searching and cutting will find your desired result:一些相当直接的字符串搜索和剪切将找到您想要的结果:

-- Substring from the first tag to the last tag, including the last tag
select substring([data], StartTag, EndTag - StartTag + LenTag)
from (
  select @getdata [data]
    -- Find the start of the first tag
    , patindex('%<LicensingReportProcessResult%', @getdata) StartTag
    -- Find the start of the second tag
    , patindex('%</LicensingReportProcessResult>%', @getdata) EndTag
    -- Find the length of the tag
    , len('<\LicensingReportProcessResult>') LenTag
) x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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