2013-04-24 51 views
1
@text = 'First +Last /(toto salary *0.07)' 

什麼是SQL查詢,以取代 「只有整個短語」 愨操作SQL正則表達式替換整個詞組的

我的意思是:

replace(@text , 'toto salary','bobo salary') 
    result : 'First +Last /(bobo salary *0.07)' 

但當:

replace(@text , 'toto','') 
NO result because not match whole phrase ('toto salary') 

換句話說,替換'* +/- ()'運算符之間的短語

+0

您使用哪種RDBMS? – 2013-04-24 08:57:37

+0

sql server 2008 r2 – Fadi 2013-04-24 13:14:08

回答

0

下可以工作:

select regexp_replace('First +Last /(toto salary *0.07)' , '(/\()toto salary([*][0-9]+[.]?[0-9]*\))','\1bobo salary\2') from dual; 

訣竅是把括號各地要繼續, 在這種情況下,/(*0.07)強制部分,然後通過添加\1\2包括那些部分在替換表達式中等等。

+1

不幸的是'regexp_replace'在SQL Server中不可用 – 2013-04-24 13:27:49

+0

所以解決方案 – Fadi 2013-04-24 15:32:42

+1

在替換前添加佔位符: 'replace('/(toto salary','/(bobo salary')' – 2013-04-24 18:38:21