1

次のような行にデータを保持する列があります。

Col1 '2118,2110,2114,2112',
Col2 '2110,2111,2112,2113,2114,2115,2117,2118,2119,2152,2153' and 
Col3 '2110,2111,2113,2114'. 

2110削除コマンドを実行して削除するときに、すべての列から置き換えたいと思い2110ます。

私が試したのは (私は col1 値に対してこれを行っています - 文字列の最初と最後にコンマを追加しているので、結果を満たすようにしています。 ',2110,' として開始および終了)

DECLARE @TagId VARCHAR(MAX)
SET @TagId = '2118,2110,2114,2112'
print 'Select Tag ' + @TagId
SET @TagId = ',' + @TagId + ','
print 'Adding comma ' + @TagId
SET @TagId = REPLACE(@TagId,',2110,',',')
print 'Replace the string ' + @TagId
SET @TagId = SUBSTRING(@TagId,1,LEN(@TagId))
print 'Replace start comma ' + @TagId
SET @TagId = SUBSTRING(@TagId,0,LEN(@TagId) -1)
print 'Replace end comma' + @TagId
SELECT @TagId

問題は、開始時に追加した開始と終了のコンマを置き換えないことです。ここで私が間違っていることを教えてください。

4

3 に答える 3

0

これを試して、

DECLARE @TagId VARCHAR(MAX)
SET @TagId = '2118,2110,2114,2112'
print 'Select Tag ' + @TagId
SET @TagId = ',' + @TagId + ','
print 'Adding comma ' + @TagId
SET @TagId = REPLACE(@TagId,',2110,',',')
print 'Replace the string ' + @TagId
SET @TagId = CASE WHEN LEN(@TagId) > 1 THEN SUBSTRING(@TagId,2,LEN(@TagId)) 
             ELSE @TagId END
print 'Replace start comma ' + @TagId
SET @TagId = SUBSTRING(@TagId,1,LEN(@TagId) -1)
print 'Replace end comma ' + @TagId
SELECT @TagId
于 2013-02-04T06:09:25.260 に答える
0

これを試して :

DECLARE @TagId VARCHAR(MAX)
SET @TagId = '2118,2110,2114,2112'
print 'Select Tag ' + @TagId
SET @TagId = ',' + @TagId + ','
print 'Adding comma ' + @TagId
SET @TagId = REPLACE(@TagId,',2110,',',')
print 'Replace the string ' + @TagId
SET @TagId = SUBSTRING(@TagId,2,LEN(@TagId))
print 'Replace start comma ' + @TagId
SET @TagId = SUBSTRING(@TagId,0,LEN(@TagId))
print 'Replace end cooma' + @TagId
SELECT @TagId

SUBSTRINGの詳細

于 2013-02-04T05:47:53.823 に答える
0

なぜ最初と最後にコンマを追加するのですか? 確かに、次のように置き換えることができます:

DECLARE @TagId VARCHAR(MAX)
SET @TagId = '2118,2110,2114,2112'
SET @TagId = REPLACE(@TagId, '2110' , '')

次に、二重コンマを削除します。

SET @TagId = REPLACE(@TagId, ',' , ',')
于 2013-02-04T05:52:55.467 に答える