MySql中substring字串截取函數用法

來源:互聯網
上載者:User

用法:

SUBSTRING(str,pos,len)
SUBSTRING(str FROM pos FOR len)
SUBSTRING(str,pos)
SUBSTRING(str FROM pos)

別名SUBSTR

截取字串str從pos開始長度為len的字串,如果不設定len參數預設擷取pos以後的所有內容
注意字串的索引是從1開始。
如果pos為負數則從字串的後面開始截取。

1.截取pos後所有的資料

 代碼如下 複製代碼

mysql> select substring('mysql database',2);
+——————————-+
| substring('mysql database',2) |
+——————————-+
| ysql database                 |
+——————————-+
1 row in set

2.截取pos後3個字元資料

 代碼如下 複製代碼

mysql> select substring('mysql database',2,3);
+———————————+
| substring('mysql database',2,3) |
+———————————+
| ysq                             |
+———————————+
1 row in set

3.看一些其它執行個體

 代碼如下 複製代碼

mysql> SELECT SUBSTRING(‘Quadratically’,5);
-> ‘ratically’
mysql> SELECT SUBSTRING(‘foobarbar’ FROM 4);
-> ‘barbar’
mysql> SELECT SUBSTRING(‘Quadratically’,5,6);
-> ‘ratica’
mysql> SELECT SUBSTRING(‘Sakila’, -3);
-> ‘ila’
mysql> SELECT SUBSTRING(‘Sakila’, -5, 3);
-> ‘aki’
mysql> SELECT SUBSTRING(‘Sakila’ FROM -4 FOR 2);
-> ‘ki’

既然講到這裡來了我再看看

substring_index

substring_index(str,delim,count)

 代碼如下 複製代碼

mysql> SELECT SUBSTRING_INDEX(‘www.111cn.net’, ‘.’, 2);
-> ‘www.111cn’
mysql> SELECT SUBSTRING_INDEX(‘www.111cn.net’, ‘.’, -2);
-> ‘111cn.net’

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.