MySQL字串函數:字串截取

來源:互聯網
上載者:User

MySQL 字串截取函數:left(), right(), substring(), substring_index()。還有 mid(), substr()。其中,mid(), substr() 等價於 substring() 函數,substring() 的功能非常強大和靈活。

1. 字串截取:left(str, length)

mysql> select left('bkjia.com', 3);
+-------------------------+
| left('bkjia.com', 3) |
+-------------------------+
| sql                     |
+-------------------------+
2. 字串截取:right(str, length)

mysql> select right('bkjia.com', 3);
+--------------------------+
| right('bkjia.com', 3) |
+--------------------------+
| com                      |
+--------------------------+
3. 字串截取:substring(str, pos); substring(str, pos, len)

3.1 從字串的第 4 個字元位置開始取,直到結束。

mysql> select substring('bkjia.com', 4);
+------------------------------+
| substring('bkjia.com', 4) |
+------------------------------+
| study.com                    |
+------------------------------+
3.2 從字串的第 4 個字元位置開始取,只取 2 個字元。

mysql> select substring('bkjia.com', 4, 2);
+---------------------------------+
| substring('bkjia.com', 4, 2) |
+---------------------------------+
| st                              |
+---------------------------------+
3.3 從字串的第 4 個字元位置(倒數)開始取,直到結束。

mysql> select substring('bkjia.com', -4);
+-------------------------------+
| substring('bkjia.com', -4) |
+-------------------------------+
| .com                          |
+-------------------------------+
3.4 從字串的第 4 個字元位置(倒數)開始取,只取 2 個字元。

mysql> select substring('bkjia.com', -4, 2);
+----------------------------------+
| substring('bkjia.com', -4, 2) |
+----------------------------------+
| .c                               |
+----------------------------------+
我們注意到在函數 substring(str,pos, len)中, pos 可以是負值,但 len 不能取負值。

4. 字串截取:substring_index(str,delim,count)

4.1 截取第二個 '.' 之前的所有字元。

mysql> select substring_index('www.bkjia.com', '.', 2);
+------------------------------------------------+
| substring_index('www.bkjia.com', '.', 2) |
+------------------------------------------------+
| www                                  |
+------------------------------------------------+
4.2 截取第二個 '.' (倒數)之後的所有字元。

mysql> select substring_index('www.bkjia.com', '.', -2);
+-------------------------------------------------+
| substring_index('www.bkjia.com', '.', -2) |
+-------------------------------------------------+
| com.cn                                          |
+-------------------------------------------------+
4.3 如果在字串中找不到 delim 參數指定的值,就返回整個字串

mysql> select substring_index('www.bkjia.com', '.coc', 1);
+---------------------------------------------------+
| substring_index('www.bkjia.com', '.coc', 1) |
+---------------------------------------------------+
| www.bkjia.com                               |
+---------------------------------------------------+

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.