SQL Server DB テーブルにvarchar(max)
フィールドがあり、このフィールド内の文字列のすべての出現箇所と、読み取り時にコンテキストを与えるために両側に 50 文字を検索したいと考えています。
以下のコードを使用してこれを行いましたが、文字列のすべての出現に対してそれを実行し、1 つのフィールドに表示されるように調整したいと思います。
declare @extract varchar(max)
set @extract =
'As Harry squelched along the deserted corridor he came across somebody who looked just as preoccupied as he was. Nearly Headless Nick, the ghost of Gryffindor Tower, was staring morosely out of a window, muttering under his breath, ". . . don''t fulfill their requirements . . . half an inch, if that . . ."
"Hello, Nick," said Harry.
"Hello, hello," said Nearly Headless Nick, starting and looking round. He wore a dashing, plumed hat on his long curly hair, and a tunic with a ruff, which concealed the fact that his neck was almost completely severed. He was pale as smoke, and Harry could see right through him to the dark sky and torrential rain outside.
"You look troubled, young Potter," said Nick, folding a transparent letter as he spoke and tucking it inside his doublet.
"So do you," said Harry.'
declare @searchterm varchar(max)
set @searchterm = '%Harry%'
declare @searchtermlength int = len(@searchterm)
declare @stringFrom int = (select PATINDEX(@searchterm,@extract) - 50 )
declare @stringto int = (select PATINDEX(@searchterm,@extract) + @searchtermlength + 50 )
declare @noChars int = @stringto - @stringfrom
select SUBSTRING(@extract,@stringFrom,@noChars)
これは返されます'As Harry squelched along the deserted corridor he came acros'
が、返してほしいです:
As Harry squelched along the deserted corridor he came acros...
...f an inch, if that . . ." "Hello, Nick," said Harry. "Hello, hello," said Nearly Headless Nick, sta
...ost completely severed. He was pale as smoke, and Harry could see right through him to the dark sky and tor...
...king it inside his doublet. "So do you," said Harry.
これはループを使用して実行できると思いますが、これまで実際にループを使用したことがないので、助けていただければ幸いです。
前もって感謝します