mysql 常用字符串函數總結

參考博客:https://www.cnblogs.com/geaozhang/p/6739303.html

名稱 功能
lower(column | str) 字符串轉小寫
upper(column | str) 字符串轉大寫
concat(column | str, column|str, …) 拼接字符串
concat_ws(separator, column | str, column|str, …), 指定拼接符拼接字符串
substr(str, pos[, length]) 返回字符串的子串,pos索引從1開始,length不指定則到末尾
substring(str, pos[, length]) 同上,索引從1開始
left(str, length) 截取左邊指定長度的子串
right(str, length) 截取右邊指定長度的子串
length(str) 獲取字符串的字節長度
char_length(str) 字符串的字符長度
instr(str, substr) 返回子串在字符串的索引位置,如果沒有找到,則返回0
lpad(str, length, padstr) 左填充
rpad(str, length, padstr) } 右填充
trim( [substr from] str ) 去除字符串首尾的指定子串,默認子串爲空格
ltrim([substr from] str) 去除字符串左邊的指定子串,默認子串爲空格
rtrim([substr from] str) 去除字符串右邊的指定子串,默認子串爲空格
replace(str, from_str, to_str) 替換全部匹配的子串
format(x , D[, local]) 格式化數字x,可用於截取小數點位數,取整等
space(N) 返回由N個空格組成的子串
repeat(str, count) 返回重複字符串count次組成的字符串
reverse(str) 反轉字符串
strcmp( str1, str2 ) 比較兩個字符串,大於返回1;等於返回0;小於返回-1

1、lower(column | str)

作用:將字符串全部轉爲小寫字母后返回。

select lower('MYSQL Course');
+-----------------------+
| lower('MYSQL_Course') |
+-----------------------+
| mysql_course          |
+-----------------------+

2、upper(column | str)

作用: 將字符串全部轉爲大寫字母后返回。

mysql> select upper('Mysql_Course');
+-----------------------+
| upper('Mysql_Course') |
+-----------------------+
| MYSQL_COURSE          |
+-----------------------+

3、concat(column|str1, column|str2, …)

作用: 拼接字符串.

mysql> select concat('first_name', 'middle_name', 'lasr_name');
+--------------------------------------------------+
| concat('first_name', 'middle_name', 'lasr_name') |
+--------------------------------------------------+
| first_namemiddle_namelasr_name                   |
+--------------------------------------------------+

(1)如果任一一個參數的值爲null,則拼接的結果爲null。

mysql> select concat('first_name', 'middle_name', 'lasr_name', null);
+--------------------------------------------------------+
| concat('first_name', 'middle_name', 'lasr_name', null) |
+--------------------------------------------------------+
| NULL                                                   |
+--------------------------------------------------------+
1 row in set (0.02 sec)

如果參數有非null非字符串的數據,會自動轉換爲字符串

mysql> select concat( 1.23, 234, 678 );
+--------------------------+
| concat( 1.23, 234, 678 ) |
+--------------------------+
| 1.23234678               |
+--------------------------+
1 row in set (0.00 sec)

4、concat_ws(separator, str, str2,…)

作用: 按照指定拼接符拼接字符串。如果有參數爲null,則忽略該null。

mysql> select concat_ws(',','first_name', 'middle_name', 'lasr_name', null);
+---------------------------------------------------------------+
| concat_ws(',','first_name', 'middle_name', 'lasr_name', null) |
+---------------------------------------------------------------+
| first_name,middle_name,lasr_name                              |
+---------------------------------------------------------------+
1 row in set (0.02 sec)

5、substr( str, pos[, lenght] )
        substring(str, pos[, length])

作用: 截取從指定位置,指定長度的子串。

mysql> select substr('mysql learning', 2,2);
+-------------------------------+
| substr('mysql learning', 2,2) |
+-------------------------------+
| ys                            |
+-------------------------------+

#substring 是 substr的別名函數。
mysql> select substring('mysql learning', 2,2);
+----------------------------------+
| substring('mysql learning', 2,2) |
+----------------------------------+
| ys                               |
+----------------------------------+

5.1、left(str, length)

作用:從左邊截取指定長度的子串。

mysql> select left('mysql learning', 2);
+---------------------------+
| left('mysql learning', 2) |
+---------------------------+
| my                        |
+---------------------------+

5.2、 right(str,length)

作用:從右邊截取指定長度的子串。

mysql> select right('mysql learning中文', 2);
+----------------------------------+
| right('mysql learning中文', 2)   |
+----------------------------------+
| 中文                             |
+----------------------------------+

6、length(str)

作用: 返回字符串的字節長度

#utf8編碼下,
mysql> select length("中文版mysql");
+--------------------------+
| length("中文版mysql")    |
+--------------------------+
|                       14 |
+--------------------------+

7、char_length(str)

作用:返回字符串的字符個數

mysql> select char_length("中文版mysql");
+-------------------------------+
| char_length("中文版mysql")    |
+-------------------------------+
|                             8 |
+-------------------------------+

8、instr(str, substr)

作用:返回子串中首次出現的位置。如果沒有找到,則返回0。

mysql> select instr('這是一個基礎測試測試','測試');
+--------------------------------------------------+
| instr('這是一個基礎測試測試','測試')             |
+--------------------------------------------------+
|                                                7 |
+--------------------------------------------------+

9、lpad(str, length, padstr)

作用:左填充。

mysql> select lpad('first mysql', 20, '####')
    -> ;
+---------------------------------+
| lpad('first mysql', 20, '####') |
+---------------------------------+
| #########first mysql            |
+---------------------------------+

10、rpad(str, length, padstr)

作用:右填充。

mysql> select rpad('first mysql', 20, '####');
+---------------------------------+
| rpad('first mysql', 20, '####') |
+---------------------------------+
| first mysql#########            |
+---------------------------------+

11、trim( [xx from] str )

作用:去除字符串首尾兩端的指定子串。默認子串爲空格。

mysql> select trim('   hello world  !   ');
+------------------------------+
| trim('   hello world  !   ') |
+------------------------------+
| hello world  !               |
+------------------------------+

#指定要去掉的子串
mysql> select trim(  'a' from  'a a  hello world  !   a a');
+-----------------------------------------------+
| trim(  'a' from  'a a  hello world  !   a a') |
+-----------------------------------------------+
|  a  hello world  !   a                        |

12、ltrim(str)

作用: 去除字符串首端的空格。

mysql> select ltrim( ' a  hello world  !  ');
+--------------------------------+
| ltrim( ' a  hello world  !  ') |
+--------------------------------+
| a  hello world  !              |
+--------------------------------+

13、rtrim([xx from] str)

作用: 去除字符串尾端的空格。

mysql> select rtrim( ' a  hello world  !  ');
+--------------------------------+
| rtrim( ' a  hello world  !  ') |
+--------------------------------+
|  a  hello world  !             |
+--------------------------------+

14、replace(str, from_str, to_str)

作用:替換全部匹配到的子串。

mysql> select replace('這是一個一個一個簡單的測試','一','二');
+----------------------------------------------------------------+
| replace('這是一個一個一個簡單的測試','一','二')                |
+----------------------------------------------------------------+
| 這是二個二個二個簡單的測試                                     |
+----------------------------------------------------------------+

15、format(x, D[,local])

作用:對數字x進行格式化。

mysql> select format(12.3456, 2);
+--------------------+
| format(12.3456, 2) |
+--------------------+
| 12.35              |
+--------------------+

mysql> select format(12.3456, 0);
+--------------------+
| format(12.3456, 0) |
+--------------------+
| 12                 |
+--------------------+

16、reverse(str)

作用:字符串反轉。

mysql> select reverse('hello world!');
+-------------------------+
| reverse('hello world!') |
+-------------------------+
| !dlrow olleh            |
+-------------------------+

17、space(count)

作用:返回n個空格組成的子串。

mysql> select concat('a',space(10), 'a');
+----------------------------+
| concat('a',space(10), 'a') |
+----------------------------+
| a          a               |
+----------------------------+

18、repeat(str, count)

作用:返回str重複n次後組成的子串。


mysql> select repeat('a', 10);
+-----------------+
| repeat('a', 10) |
+-----------------+
| aaaaaaaaaa      |
+-----------------+

19、strcmp(str1, str2)

作用:比較兩個字符串的大小。如果左邊的字符串大於右邊的字符串則返回1,相等返回0,小於則返回-1。

mysql> select strcmp('a', 'bb');
+-------------------+
| strcmp('a', 'bb') |
+-------------------+
|                -1 |
+-------------------+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章