是否可以從LIKE查詢的結果中獲取片段字符串?Sql server LIKE從結果中獲取片段
我不想嘗試一些奇特的東西。我想要的只是TRIM查詢字符串和左右兩個字符(即50個字符)。
例如,如果像查詢 「狐狸」 返回
"The quick brown fox jumps over the lazy dog"
,我想
"brown fox jumps"
更新,這就是我想出了。看來工作
declare @query nvarchar(100), @str nvarchar(100), @HowManyCharsToInclude int, @startIndex int, @endIndex int, @cutfromLeft int, @cutFromRight int
set @HowManyCharsToInclude = 10
set @str='The quick brown fox jumps over the lazy dog';
set @query = 'fox'
set @startIndex = (SELECT PATINDEX('%'+ @query+ '%', @str))
set @endIndex = (@startIndex + LEN(@query))
--set left, right cut
if(@startIndex>@HowManyCharsToInclude)
begin
set @cutfromLeft = @HowManyCharsToInclude
end
else
begin
set @cutfromLeft = @startIndex
end
--set right cut
if(LEN(@str) - @endIndex>@HowManyCharsToInclude)
begin
set @cutfromRight = @HowManyCharsToInclude
end
else
begin
set @cutfromRight = (LEN(@str) - @endIndex)
end
select SUBSTRING(@str, @[email protected], LEN(@query)+ @cutFromRight + @cutfromLeft)
因爲它的工作原理,如果它提出你想要的響應時間,我想你有你的答案。 – xQbert