2012-02-26 52 views
1

在我的領域之一,我有一個看起來像SQL刪除前導連字符( - )從DB和刪除空格連續

- Data with spaces, some other data, . 

我把數據。在最後,你可以看到它有雙空白

我想刪除 - 並刪除任何和所有雙(或多個空格)。留下一個空白的結果。

並不在列的所有數據具有領先連字符( - ) 其中有些是

Data with spaces, some other data 
Data with double spaces, some other data . (double space at end) 
    Data with leading double space, some other data 
- Some data with hyphen, and double space 
    - double space leading hyphen, some other data 

這些都是在DB的一些變化。我試圖手動修復每一個,但修改條目需要很長時間。

回答

1

試試這個:

rtrim(ltrim(REPLACE (COLUMN_NAME, '-', ' '))) 

,如果您需要在最後一個額外的空間,我聽不懂,但如果你只需要添加+」'

+0

謝謝你做到了 – 2012-02-26 20:21:08

1

您可以嘗試合併一些core functions以獲得您想要的結果。

例如,查詢

select trim(substr('- Data with spaces, some other data, ', 2)) 

輸出Data with spaces, some other data,

首先,我通過返回一個子除去-,然後我修剪空白。

編輯:檢查出領導-字符

select trim(
    case 
     when (substr(FIELD, 1, 1) in ('-')) 
     then substr(FIELD, 2) 
     else 
     FIELD 
    end) 

in ('-')是要排除更多的符號,當然如此。 in ('-', '+', ',')

+0

並非所有的數據都領先 - – 2012-02-26 19:18:35

+0

@CocoaDev,我更新了我的答案。 – 2012-02-26 20:09:47

0

這樣的事情可以工作。雖然有點亂,所以我不確定這是不是你想要的。你可能需要一個像cte一樣的遞歸函數來刪除2個以上的空格。但是,這解決了所提出的問題。

select 
case SUBSTRING('- Data with spaces, some other data, .', 1,1) 
    when '-' then REPLACE(
     SUBSTRING('- Data with spaces, some other data, .',2, 
      LEN('- Data with spaces, some other data, .')-1) 
     ,' ',' ') 
    else REPLACE('- Data with spaces, some other data, .',' ',' ') 
end