MySQL字串函數,mysql字串

來源:互聯網
上載者:User

MySQL字串函數,mysql字串
字串大小寫轉換

MySQL 字串大小寫轉化函數有兩對: lower(), uppper() 和 lcase(), ucase()

mysql> select lower('DDD');+--------------+| lower('DDD') |+--------------+| ddd          |+--------------+mysql> select upper('ddd');+--------------+| upper('ddd') |+--------------+| DDD          |+--------------+mysql> select lcase('DDD');+--------------+| lcase('DDD') |+--------------+| ddd          |+--------------+mysql> select ucase('ddd');+--------------+| ucase('ddd') |+--------------+| DDD          |+--------------+

通常情況下,我選擇 lower(), upper() 來轉換字串大小寫,因為這和其他資料庫中函數相相容。

清除字串首尾空格

MySQL 中的清除字串首尾空格函數有三個: ltrim(), rtrim(), trim()

mysql> select concat('.', ltrim(' ddd '), '.');+----------------------------------+| concat('.', ltrim(' ddd '), '.') |+----------------------------------+| .ddd .                           |+----------------------------------+mysql> select concat('.', rtrim(' ddd '), '.');+----------------------------------+| concat('.', rtrim(' ddd '), '.') |+----------------------------------+| . ddd.                           |+----------------------------------+mysql> select concat('.', trim(' ddd '), '.');+---------------------------------+| concat('.', trim(' ddd '), '.') |+---------------------------------+| .ddd.                           |+---------------------------------+

MySQL 中的 trim 字串函數,實在是強大。它不僅能消除字串首尾部的空格,還可以消除我們指定的任一字元。ltrim(), rtrim() 只是它的一個功能子集。來看下 trim 函數的完整文法:

1. trim([{both | leading | trailing} [remstr] from] str)2. trim([remstr from] str)

1. 清除字串首部字元。

mysql> select trim(leading '.' from '..ddd..');+----------------------------------+| trim(leading '.' from '..ddd..') |+----------------------------------+| ddd..                            |+----------------------------------+

2. 清除字串尾部字元。

mysql> select trim(trailing '.' from '..ddd..');+-----------------------------------+| trim(trailing '.' from '..ddd..') |+-----------------------------------+| ..ddd                             |+-----------------------------------+

3. 清除字串首尾部字元。

mysql> select trim(both '.' from '..ddd..');+-------------------------------+| trim(both '.' from '..ddd..') |+-------------------------------+| ddd                           |+-------------------------------+mysql> select trim('.' from '..ddd..');+--------------------------+| trim('.' from '..ddd..') |+--------------------------+| ddd                      |+--------------------------+

trim() 預設清除字串首尾部的空格。

字串截取

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

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

mysql> select left('sqlstudy.com', 3);+-------------------------+| left('sqlstudy.com', 3) |+-------------------------+| sql                     |+-------------------------+

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

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

3. 字串截取:substring(str, pos); substring(str, pos, len)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

相關文章

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.