MySQL字串函數使用範例(字串截取)

來源:互聯網
上載者:User
注意:本文出自 “阿飛”的部落格 ,如果要轉載本文章,請與作者聯絡!並註明來源: http://blog.csdn.net/faye0412/article/details/7915300

這兩天一直在協助同事做資料匯入模組,也遇到了不少問題,比如前一篇文章裡提到的CSV大檔案匯入MYSQL,匯入MYSQL後,關鍵字什麼的資料整理和複製等等,還有sql中涉及到了一些字串的截取等處理....

本文就MYSQL中對字串截取操作做個小結,將字串函數使用做個使用說明並附各種例子,具體如下:

————————————————————————————————————————

在這裡只對常用的MySQL字串截取函數進行舉例說明:left(), right(), substring()和 substring_index()。 還有 mid(), substr()。 

其中,mid(),substr() 等價於 substring() 函數,substring() 的功能非常強大和靈活。各個例子如下所示(建表SQL略):

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

mysql> select left('starhub.com', 7);
+-----------------------------+
| left('starhub.com', 7)   |
+-----------------------------+
| starhua                          |
+-----------------------------+

2. 字串截取:right(str, length)

mysql> select right('starhub.com', 3);
+--------------------------+
| right('starhub.com', 3) |
+--------------------------+
| com                      |
+--------------------------+

3. 字串截取:substring(str, pos); substring(str, pos, len)
3.1 從字串的第 4 個字元位置開始取,直到結束。

mysql> select substring('starhub.com', 4);
+-------------------------------------+
| substring('starhub.com', 4)  |
+-------------------------------------+
| rhub.com                                 |
+-------------------------------------+

3.2 從字串的第 4 個字元位置開始取,只取 2 個字元。

mysql> select substring('starhub.com', 4, 2);
+----------------------------------------+
| substring('starhub.com', 4, 2) |
+----------------------------------------+
| rh                                                   |
+----------------------------------------+

3.3 從字串的第 4 個字元位置(倒數)開始取,直到結束。

mysql> select substring('starhub.com', -4);
+--------------------------------------+
| substring('starhub.com', -4) |
+--------------------------------------+
| .com                                           |
+--------------------------------------+

3.4 從字串的第 4 個字元位置(倒數)開始取,只取 2 個字元。

mysql> select substring('starhub.com', -4, 2);
+-----------------------------------------+
| substring('starhub.com', -4, 2) |
+-----------------------------------------+
| .c                                                     |
+-----------------------------------------+

我們注意到在函數 substring(str,pos, len)中, pos 可以是負值,但 len 不能取負值。

4. 字串截取:substring_index(str,delim,count)
4.1 截取第二個 '.' 之前的所有字元。

mysql> select substring_index('www.starhub.com.cn', '.', 2);
+------------------------------------------------------------+
| substring_index('www.starhub.com.sg', '.', 2) |
+-------------------------------------------------------------+
| www.starhub                                                          |
+-------------------------------------------------------------+

4.2 截取第二個 '.' (倒數)之後的所有字元。

mysql> select substring_index('www.starhub.com.cn', '.', -2);
+-------------------------------------------------+
| substring_index('www.starhub.com.sg', '.', -2) |
+-------------------------------------------------+
| com.sg                                          |
+-------------------------------------------------+

4.3 如果在字串中找不到 delim 參數指定的值,就返回整個字串

mysql> select substring_index('www.starhub.com.cn', '.coc', 1);
+-----------------------------------------------------------------+
| substring_index('www.starhub.com.sg', '.con', 1) |
+------------------------------------------------------------------+
| www.starhub.com.cn                                                   |
+------------------------------------------------------------------+
 

5. 其他參考

更多函數和資源請參考:

http://dev.mysql.com/doc/refman/5.1/zh/functions.html

http://blog.51yip.com/mysql/965.html

http://database.51cto.com/art/200509/2479.htm

聯繫我們

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