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 |
+---------------------------------------------------+
MySQL字符串函数相关推荐
- MySQL字符串函数substring:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- 详解MySQL字符串函数重复REPEAT()、反转REVERSE()、格式化FORMAT()、返回空格SPACE()函数用法(四)
文章目录 一.重复函数REPEAT() 二.反转函数REVERSE() 三.格式化函数FORMAT() 四.反转函数SPACE() 前言:在实际的项目开发中有时会有对数据库某些字段去重等需求,这种场景 ...
- 详解MySQL字符串函数CONCAT()、CONCAT_WS()、INSERT()、REPLACE()、LOWER()、UPPER()用法(二)
文章目录 一.合并字符串函数CONCAT()和CONCAT_WS() 1.CONCAT() 2.CONCAT_WS() 二.替换字符串INSERT()和REPLACE 1.INSERT() 2.REP ...
- MySQL 字符串函数:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- 详解MySQL字符串函数TRIM()、填充函数LPAD()、RPAD()、字符串长度LENGTH()用法(三)
文章目录 一.删除空格函数TRIM().LTRIM().RTRIM() 1.TRIM() 2.LTRIM() 3.RTRIM() 二.填充函数LPAD().RPAD() 1.LPAD() 2.RPAD ...
- mysql数字字符串教程_很全面的mysql字符串函数的教程
说明:如果结果的长度大于服务器参数max_allowed_packet,字符串值函数返回NULL. 对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符 ...
- mysql 字符串函数大全_mysql 字符串函数大全
返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL.mysql> select ASCII('2'); -> 50mysql& ...
- mysql 字符串函数
用sql久了, 自然就越发觉得sql的方便, 于是研究研究sql的内置函数 1. CONCAT(S1, S2, Sn) 作用:连接S1,S2...为一个字符串 例子: SELECTCONCAT(Pro ...
- mysql字符串函数混合使用_MySql字符串函数使用技巧
1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my_con ...
最新文章
- C# MoreLinq 扩展安装
- mongodb和python交互
- 实现虚拟xp与主机xp之间的文件共享
- Maven编译jar出现:无法确定 T 的类型参数的异常的原因和处理方案
- 全文搜索技术—Lucene
- 回《笔试常见的“阶乘”编程题,你写对了么?》
- 007-SDK框架之LYWSDKInterfaceProtocol.cpp
- SpringSecurity简单应用(二)
- 大牛手把手教你!2021Java最新大厂面试真题
- 2寸的照片长宽各是多少_贵州公务员考试照片尺寸要求是多少
- 进程间通信-Queue(python版)
- 又出问题!特斯拉回应384万天价充电费:系统错误,正在修复
- WebAssembly系列1-从 ASM.JS 到 WebAssembly
- 乘法器的Verilog HDL实现
- leetcode刷题--python
- Welcome-to-Swift-13继承(Inheritance)
- ENVI中FLAASH大气校正中的Atmosphere Model选项各含义
- JAVA对接发送SMS短信服务
- kali初讲——Metasploit工具MSF初学
- Unity 2D 摄像机平滑跟随