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字符串函数、日期时间函数
MySQL字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH 获取长度(字符为单位) 2.FORMAT 格式化 3.INSERT 替换的方式插入 4.INSTR 获取位 ...
- MySQL字符串函数substring:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- mysql字符串函数(转载)
对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...
- 【转】mysql字符串函数
对于针对字符串位置的操作,第一个位置被标记为1(即:第一个字母索引为1). ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL ...
- mysql 字符串函数
对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...
- MySQL字符串函数:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- Mysql 字符串函数 详解
字符串函数是最常用的一种函数了,如果大家编写过程序的话,不妨回过头去看看自己使用过的函数,可能会惊讶地发现字符串处理的相关函数占已使用过的函数很大一部分.MySQL中字符串函数也是最丰富的一类函数,表 ...
- MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数
一.常见字符串函数: 1.CHAR_LENGTH 获取长度(字符为单位) 2.FORMAT 格式化 3.INSERT 替换的方式插入 4.INSTR 获取位置 5.LEFT/RIGHT 取左 ...
- mysql 字符串函数、分组函数
字符串函数 1.concat 函数 drop table test;create table test(id int(4), name varchar(10), sex char(2));insert ...
随机推荐
- javascript div跟随鼠标移动
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- php读取excel,以及php打包文件夹为zip文件
1.把文件下载到本地,放在在Apache环境下2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码3.访问zip.php的功能是把生成的 ...
- php函数声明的简单实例
<?phpecho table(10,5,500);echo table(2,2,400); //函数调用 function table($row,$col,$width){ //通过函数标记t ...
- Hierarchy Viewr 配合 adb 命令 查看窗口属性
Hierarchy Viewr 可以看到当前 的 窗口层次如下
- 将图片文件转换为.py文件
最近用wxpython写了一个脚本,其中要给窗体设置图标文件,需要单独的一个ico文件,这样就比较影响美观,另外打包的时候还要将图标文件一起打包很繁琐.这时候看到wxpython文件有一个工具img2 ...
- 使用linux的GDB打印STL(vector,map,set..................)
在linux用gdb或者cgdb计较不爽的地方是无法打印STL的东西,所有啊去网上找了找解决方案https://www.douban.com/note/182826844/?qq-pf-to=pcqq ...
- Objective-C 入门(给新人的)
http://www.hengxinsoft.com/2010/12/objective-c-%E5%85%A5%E9%97%A8%EF%BC%88%E7%BB%99%E6%96%B0%E4%BA%B ...
- bzoj 1079: [SCOI2008]着色方案 DP
1079: [SCOI2008]着色方案 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 803 Solved: 512[Submit][Status ...
- 跨终端Web之Hybrid App
Native App(以下简称Native)和Mobile Web(以下简称Web)二者混合开发的产物被称为Hybrid App(以下简称Hybrid).Hybrid并不是什么新概念,最早可以追溯到S ...
- java4中创建内对象的方法
在java程序中,对象可以被显式地或者隐式地创建.四种显式的创建对象的方式: ● 用new语句创建对象 ● 运用反射手段,调用java.lang.Class 或者 java.lang. ...