Home > Database > Mysql Tutorial > MySQL字符串函数

MySQL字符串函数

WBOY
Release: 2016-06-07 15:46:49
Original
1198 people have browsed it

字符串大小写转换 MySQL 字符串大小写转化函数有两对: lower(), uppper() 和 lcase(), ucase() mysql select lower(DDD);+--------------+| lower(DDD) |+--------------+| ddd |+--------------+mysql select upper(ddd);+--------------+| upper(ddd) |+-

字符串大小写转换

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          |
+--------------+
Copy after login

通常情况下,我选择 lower(), upper() 来转换字符串大小写,因为这和其他数据库中函数相兼容。

清除字符串首尾空格

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

<pre class="brush:php;toolbar:false">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.                           |
+---------------------------------+
Copy after login

MySQL 中的 trim 字符串函数,实在是强大。它不仅能消除字符串首尾部的空格,还可以消除我们指定的任意字符。ltrim(), rtrim() 只是它的一个功能子集。来看下 trim 函数的完整语法:

<pre class="brush:php;toolbar:false">1. trim([{both | leading | trailing} [remstr] from] str)
2. trim([remstr from] str)
Copy after login

1. 清除字符串首部字符。

<pre class="brush:php;toolbar:false">mysql> select trim(leading '.' from '..ddd..');
+----------------------------------+
| trim(leading '.' from '..ddd..') |
+----------------------------------+
| ddd..                            |
+----------------------------------+
Copy after login

2. 清除字符串尾部字符。

<pre class="brush:php;toolbar:false">mysql> select trim(trailing '.' from '..ddd..');
+-----------------------------------+
| trim(trailing '.' from '..ddd..') |
+-----------------------------------+
| ..ddd                             |
+-----------------------------------+
Copy after login

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

<pre class="brush:php;toolbar:false">mysql> select trim(both '.' from '..ddd..');
+-------------------------------+
| trim(both '.' from '..ddd..') |
+-------------------------------+
| ddd                           |
+-------------------------------+

mysql> select trim('.' from '..ddd..');
+--------------------------+
| trim('.' from '..ddd..') |
+--------------------------+
| ddd                      |
+--------------------------+
Copy after login

trim() 默认清除字符串首尾部的空格。

字符串截取

MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。

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

<pre class="brush:php;toolbar:false">mysql> select left('sqlstudy.com', 3);
+-------------------------+
| left('sqlstudy.com', 3) |
+-------------------------+
| sql                     |
+-------------------------+
Copy after login

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

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

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

3.1 从字符串的第 4 个字符位置开始取,直到结束。

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

3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。

<pre class="brush:php;toolbar:false">mysql> select substring('sqlstudy.com', 4, 2);
+---------------------------------+
| substring('sqlstudy.com', 4, 2) |
+---------------------------------+
| st                              |
+---------------------------------+
Copy after login

3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。

<pre class="brush:php;toolbar:false">mysql> select substring('sqlstudy.com', -4);
+-------------------------------+
| substring('sqlstudy.com', -4) |
+-------------------------------+
| .com                          |
+-------------------------------+
Copy after login

3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。

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

我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。

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

4.1 截取第二个 '.' 之前的所有字符。

<pre class="brush:php;toolbar:false">mysql> select substring_index('www.sqlstudy.com.cn', '.', 2);
+------------------------------------------------+
| substring_index('www.sqlstudy.com.cn', '.', 2) |
+------------------------------------------------+
| www.sqlstudy                                   |
+------------------------------------------------+
Copy after login

4.2 截取第二个 '.' (倒数)之后的所有字符。

<pre class="brush:php;toolbar:false">mysql> select substring_index('www.sqlstudy.com.cn', '.', -2);
+-------------------------------------------------+
| substring_index('www.sqlstudy.com.cn', '.', -2) |
+-------------------------------------------------+
| com.cn                                          |
+-------------------------------------------------+
Copy after login

4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串

<pre class="brush:php;toolbar:false">mysql> select substring_index('www.sqlstudy.com.cn', '.coc', 1);
+---------------------------------------------------+
| substring_index('www.sqlstudy.com.cn', '.coc', 1) |
+---------------------------------------------------+
| www.sqlstudy.com.cn                               |
+---------------------------------------------------+
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template