gpt4 book ai didi

sql - 从 SQL 中的字符串中提取子字符串

转载 作者:行者123 更新时间:2023-12-04 14:05:37 26 4
gpt4 key购买 nike

我需要提取一个被 ***[some text] 包围的文本字符串,如下例所示:

some text
some text
***[some text]
THIS SHOULD BE EXTRACTED
***[some text]
some text
some text
some text
some text
some text
***[some text]
THIS SHOULD BE EXTRACTED TOO
***[some text]
some text

输出应该是:
THIS SHOULD BE EXTRACTED
THIS SHOULD BE EXTRACTED TOO

我试过 PATINDEX像这里,但找不到提取字符串的方法。
PATINDEX('%[*][*][*][[]%]%%[*][*][*][[]%]%',@Text)

我期待听到任何建议。

最佳答案

对于评论中提出的更简单的案例,您可以这样做

;WITH T(C) AS
(
SELECT '
some text
some text
***[some text 1]
THIS SHOULD BE EXTRACTED
***[some text 2]
some text
some text
some text
some text
some text
***[some text 1]
THIS SHOULD BE EXTRACTED TOO
***[some text 2]
some text'
)
SELECT col.value('.','varchar(max)')
FROM T
CROSS APPLY (SELECT CAST('<a keep="false">' +
REPLACE(
REPLACE(C,'***[some text 2]','</a><a keep="false">'),
'***[some text 1]','</a><a keep="true">') +
'</a>' AS xml) as xcol) x
CROSS APPLY xcol.nodes('/a[@keep="true"]') tab(col)

关于sql - 从 SQL 中的字符串中提取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5803828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com