SQL學習之替換函式replace()的使用

2021-09-11 18:01:41 字數 1233 閱讀 2235

為了加強sql的學習,最近在練習sql語句的編寫,學習過程中才發現對於sql的編寫,還是有很多東西需要更進一步加強學習的。這裡總結一下,函式replace()的使用。

語法replace ( string_expression , string_pattern , string_replacement )

引數string_expression 要搜尋的字串表示式。string_expression 可以是字元或二進位制資料型別。

string_pattern 是要查詢的子字串。string_pattern 可以是字元或二進位制資料型別。string_pattern 不能是空字串 ('')。

string_replacement 替換字串。string_replacement 可以是字元或二進位制資料型別。

返回型別

如果其中的乙個輸入引數資料型別為 nvarchar,則返回 nvarchar;否則 replace 返回 varchar。

如果任何乙個引數為 null,則返回 null

例如:update tb1 set f1=replace(f1, 'abc', 'def'); 

replace(str,from_str,to_str) 

在字串 str 中所有出現的字串 from_str 均被 to_str替換,然後返回這個字串

mysql replace用法

1.replace into 

replace into table (id,name) values('1','aa'),('2','bb')
此語句的作用是向表table中插入兩條記錄。如果主鍵id為1或2不存在 

就相當於 

insert into table (id,name) values('1','aa'),('2','bb')
如果存在相同的值則不會插入資料

2.replace(object,search,replace) 

把object**現search的全部替換為replace 

select replace('www.baidu.com','w','ww')--->wwwwww.baidu.com
例:把錶table中的name欄位中的aa替換為bb 

update table set name=replace(name,'aa','bb')

SQL學習之first 函式

示例資料 使用菜鳥教程 sql 中的first 函式返回指定的列中第乙個記錄的值 select first column name from table name 注意 只有 ms access 支援 first 函式 雖然其它資料庫中沒有實現該函式,不過我們我們可以使用limit語句達到同樣的效果...

SQL學習之last 函式

示例資料 使用菜鳥教程 sql 中的last 函式返回指定的列中最後乙個記錄的值 select last column name from table name 注意 只有 ms access 支援 last 函式 雖然其它資料庫中沒有實現該函式,不過我們我們可以使用limit語句達到同樣的效果 s...

SQL學習之round 函式

菜鳥教程 sql 中的len 函式用於把數值四捨五入為指定的小數字數 select round column name decimals from table name 引數 描述column name 必需。要捨入的數值或字段 decimals 可選。設定要返回的小數字數。預設為0 round x...