2014-03-18 88 views
-1

我必須做一個困難的文字處理。如何動態更改如下例子?Sql高級替換

示例:/你好/嬰兒/ deneme// hello2/

輸出:(/你好/)嬰兒(/ deneme /)(/ hello2 /)

+0

您使用了哪些數據庫產品? –

+0

Sql 2008 R2 .... – user2416205

+0

請參閱http://stackoverflow.com/questions/17365222/sql-server-2008-update-and-replace-part-of-a-string –

回答

0

這是一個非常基本的解決方案,但它爲你的作品已經給(SQL Fiddle here)的情況下:

SELECT 
    in_str, 
    (
    -- If the string starts with '/', prepend '(' 
    CASE WHEN in_str LIKE '/%' THEN '(' ELSE '' END 
    -- Replace/after a space with (/ 
    + REPLACE(
     -- Replace/before by a space with /) 
     REPLACE(in_str, ' /', ' (/'), 
     '/ ', '/) ' 
    ) 
    -- If the string ends with '/', append ')' 
    + CASE WHEN in_str LIKE '%/' THEN ')' ELSE '' END 
) AS out_str 
FROM table1; 

如果table1具有以下in_str值這會給你相應的out_str值:

in_str     out_str 
------------------------ ------------------------------ 
/one/ two /three/ /four/ (/one/) two (/three/) (/four/) 
one /two/ /three/   one (/two/) (/three/) 
/one/ /two/ three   (/one/) (/two/) three 
//one/// two//  (//one (/) (//) two/) (/) 

我已經包括最後一個演示一些邊緣情況。還要注意,這隻能處理/字符,後面緊跟一個空格或字符串的開頭或結尾。其他空白字符,如換行符和製表符不處理。例如,如果你有這樣的字符串(其中表示換行和選項卡):

/one/⇒/two/⏎ 
/three/⏎ 

...你會得到的輸出是這樣的:

(/one/⇒/two/⏎ 
/three/⏎ 

您可以處理這些場景還有REPLACE的功能,但這是一個兔子洞,你必須自己跳下去。

+0

THX :(但現在下代碼 實施例的工作: [Dealer_Type]在( 'XX', 'XXX')和[Is_Success] = '1' 和[Product_Full_Name]在(N'xxx」) (['xx'],['xxx'])和[Is_Success] = ['1']和[N ['xxx']中的[Product_Full_Name])中的輸出: [Dealer_Type] – user2416205