MySQL 字符串截取函数,字段截取,字符串截取
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 | +---------------------------------------------------+ 4.4 截取一个表某个字段数据的中间值 如该字段数据为 1,2,3 mysql> select substring_index(substring_index(该字段, ',', 2) , ',', -1) from 表名; +--------------------------------------------------------------+ | substring_index(substring_index(该字段, ',', 2); , ',', -1)| +--------------------------------------------------------------+ | 2 | +--------------------------------------------------------------+
MySQL 字符串截取函数,字段截取,字符串截取相关推荐
- java 中文怎么截取_Java截取字符串的函数且保证中文截取正常
SUBSTRING 返回字符.binary.text 或 image 表达式的一部分.有关可与该函数一起使用的有效 Microsoft® SQL ...
- MySQL中concat函数(连接字符串)
MySQL中concat函数(连接字符串) MySQL中concat函数 使用方法: concat(str1,str2,-) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 ...
- python 字符串函数split_字符串截取函数split(python截取字符串)
问题说split 的用发明: 我知道c里有个分割字符串的函数,我现在要你们自己写一个给我,不要用 最佳中域教育李腾飞 巴菲特案因为字符串里的\\到了split里,需要分别转义,那么自然就需要用\\ \ ...
- mysql 处理文件函数_mysql的字符串处理函数
一.简明总结 ASCII(char) 返回字符的ASCII码值 BIT_LENGTH(str) 返回字符串的比特长度 CONCAT(s1,s2-,sn) 将s1,s2-,sn连接成字符串 CONCAT ...
- php 操作字符串的函数,常用php字符串处理函数整理笔记
常用php字符串处理函数整理笔记 龙行 PHP 2018-11-20 1421 0评论 1,查找字符串位置的函数(不适合用于汉字查找) strpos(str,find,[int ...
- python字符串find函数-python常见字符串处理函数与用法汇总
本文实例讲述了python常见字符串处理函数与用法.分享给大家供大家参考,具体如下: 1.find 作用:在一个较长字符串中查找子串.返回子串所在位置的最左端索引,如果没有找到则返回-1.如果指定 b ...
- 字符串查找函数 php,php字符串查找函数(strrpos与strchr)
本节内容: php中的字符串函数strrpos与strchr,实现字符串查找方法. strchr(等同于strstr): 语法: string strstr(string haystack,strin ...
- linux下通过字符串调用函数,linux中字符串转换函数 simple_strtoul
转自 http://blog.csdn.net/tommy_wxie/article/details/7480087 Linux内核中提供的一些字符串转换函数: lib/vsprintf.c 1. u ...
- python字符串处理函数汇总_python字符串函数总结
#startswith(),检查字符串是否是以指定字符串开头,是,返回True,否则,返回False#可以加指定起始结束位置, str2 = "this is a test for a li ...
最新文章
- Protractor测试环境搭建
- 大流量网站的底层系统架构
- 数据库防火墙onefirewall,如何防范黑客物理删除?避免携程事件再次上演
- Winform中对DevExpress的RadopGroup的Description、Value、Tag、Text的理解与使用
- 使用QT创建PythonGUI程序
- SAP云平台里Run index.html和Run as SAP Fiori Launchpad的区别
- SAP Fiori里两种锁机制(lock)的实现
- 字节数组byte[]和整型,浮点型数据的转换——Java代码
- html如何转换成中文,html中文乱码怎么解决怎么造成如何避免中文乱码
- Ps“反转负冲”人像处理一例
- 2023年全国最新二级建造师精选真题及答案46
- MSHR(Miss Status Handling Register)
- ubuntu安装zsh及环境配置
- 玩转B2B平台的商业模式你选对了吗?
- java分页查询querymap_ES Java API_基于search template实现按品牌分页查询模板
- Linux下如何拷贝隐藏文件
- mac外接显示器 竖屏 黑苹果_借人气问一个黑苹果外接显示器的问题
- Altium Designer 20中创建网络类、隐藏网络连线
- 硅谷创业教父Paul Graham:如何创造财富
- UVM学习笔记(一)工厂、phase机制、config