天天看點

SQL中的replace函數

REPLACE ( string_replace1 , string_replace2 , string_replace3 )

參數解析:
    string_replace1 待搜尋的字元串表達式。string_replace1 可以是字元資料或二
    進制資料
    string_replace2 待查找的字元串表達式。string_replace2 可以是字元資料或二
    進制資料。
    string_replace3 替換用的字元串表達式。string_replace3 可以是字元資料或二
    進制資料。
      

  

執行個體

字元串類型參數:

SELECT REPLACE('abcdefg bcd','bcd','xxx')
      

結果為:axxxefg xxx

二進制類型參數:

SELECT REPLACE(100111001101,111,000)
SELECT REPLACE(100111001101,111,0)
      

結果為:100101

如果參數是支援的字元資料類型之一,并且在string_replace1 中能夠找到 
string_replace2,那麼傳回替換後的字元串;反之, 傳回 string_replace1;
如果參數是支援的 binary 資料類型之一,則傳回二進制資料。
      

  這個函數有一點不足是不支援 text,ntext類型字段的替換