2016-12-08 5 views
0

データが入っていて、 '@xx'とマークされたIDを持つ数千の文字列があります。文字列に '|'それはIDを持つデータがより多く存在するということです。SQLの文字列の一部を置換する

特定のIDを含むすべてのIDを見つけて置き換えるにはどうすればよいですか?

[email protected] -> [email protected] 
[email protected] -> [email protected] 
[email protected]|[email protected]|[email protected] -> [email protected]|[email protected]|[email protected] 

'1 @'、(列を置き換えます。私は24 @ 1 @に変更したい場合は私が期待される出力と一部exampels は241

@に変更する11 @たくありません、 '@ 24')は動作しません。@ 11は@ 241に変更されるからです。だから私はそれが文字列の終わりかどうか、または文字列が '|'で終わるかどうかを知る必要があります。

+0

期待される出力を表示 – TheGameiswar

+0

期待通りの出力で編集しました – thatsIT

答えて

2

クイックや汚れを試してみていますが、同じような何か行うことができます。そうしないとあなたがに見てする必要があります

-- Update the end bits 
UPDATE table 
SET field = LEFT('field', LEN('field') -2) + '@24' 
WHERE field LIKE '%@1'; 

-- Update the in between bits 
UPDATE table 
SET field = REPLACE(field, '@1|', '@24|') 
WHERE field LIKE '%@1|%'; -- This where statement is optional due to the nature of the REPLACE. 

をREGEXの魔法の世界。これが何度も実行したいと思ったら、私は確かにそのことを考えています。これが単なる1回限りのものなら、meh。それは木曜日です、私はそれを有効な言い訳と呼んでいます。

1

この

declare @t table(col varchar(100)) 
insert into @t 
select '[email protected]' union all 
select '[email protected]' union all 
select '[email protected]|[email protected]|[email protected]' 


select col,case when new_col like '%|' then 
       substring(new_col,1,len(new_col)-1) 
      else 
       new_col end as new_col 
from 
(
select col, 
case when col+'|' like '%@1|%' then 
Replace(col+'|', '@1|', '@24|') else col end as new_col 
from @t 
) as t 

結果

col           new_col 
--------------------------------------------- ------------ 
[email protected]         [email protected] 
[email protected]         [email protected] 
[email protected]|[email protected]|[email protected] [email protected]|[email protected]|[email protected] 
+1

それぞれが '@ xx'値を持つ複数の' | '区切りセクションを持つ文字列はどうですか?あなたが答えた後、その編集が追加されましたか? – 3N1GM4

+0

いいえ、編集されていません – thatsIT

+0

中間のものに '@ 1 |'が含まれているとどうなりますか?その結果、途中で '|'も取り除かれませんか? – Jens

関連する問題