SQL Server 2008でストアドプロシージャを作成しようとしていますが、テーブルのエントリの不要なスペースを削除する必要があります。テーブルのエントリを3つのタイプに分類しました。ストアドプロシージャは、次のように1文字の周りのスペースを削除する必要があります。
A G M words
にAGM words
words A G M words
にwords AGM words
A G words
にAG words
ストアドプロシージャに従ってみました。
CREATE proc At1 @name nvarchar(100)
as
declare @start int
declare @temp1 nvarchar(100)
declare @temp nvarchar(100)
declare @NthPosition int
declare @N int
set @N=LEN(@name)
set @start=1
set @temp1=''
set @temp=''
set @NthPosition=charindex(' ',@name,@start)
if(@NthPosition<>0)
begin
while (@NthPosition<>0 and @N<>0)
begin
set @temp1=SUBSTRING(@name,@start,@NthPosition-1)
if(@temp<>'')
begin
if(len(@temp1)=1)
begin
set @temp=(@temp+@temp1)
end
else
begin
set @temp=(@temp+' '+@temp1)
end
end
else
begin
set @temp=@temp1
end
set @start=@NthPosition+1
set @N=@N-@NthPosition
set @NthPosition=0
set @NthPosition=CHARINDEX(' ',@name,@start)
end
end
else
begin
select @name
end
select @temp
GO
そして私は使用しました、
exec At1 'apple A G M mango'
私の期待する結果:apple AGM mango
しかし、私の実際の結果:apple
エラーがどこにあるのかわかりません。この点に関する提案はもっと役に立ちます。スペースを空ける計算列を使用しようとしましたが、パターン#3の解決策を見つけることができました。3つのパターンすべてに適した計算列の定義を組み立てることができません。私に役立つ