MySQL字符串函数substring:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。
1. 字符串截取:left(str, length)
mysql> select left('example.com', 3);
+-------------------------+
| left('example.com', 3) |
+-------------------------+
| exa |
+-------------------------+
2. 字符串截取:right(str, length)
mysql> select right('example.com', 3);
+--------------------------+
| right('example.com', 3) |
+--------------------------+
| com |
+--------------------------+
实例:
#查询某个字段后两位字符
select right(last3, 2) as last2 from historydata limit 10;
#从应该字段取后两位字符更新到另外一个字段
update `historydata` set `last2`=right(last3, 2);
3. 字符串截取:substring(str, pos); substring(str, pos, len)
3.1 从字符串的第 4 个字符位置开始取,直到结束。
mysql> select substring('example.com', 4);
+------------------------------+
| substring('example.com', 4) |
+------------------------------+
| ple.com |
+------------------------------+
3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。
mysql> select substring('example.com', 4, 2);
+---------------------------------+
| substring('example.com', 4, 2) |
+---------------------------------+
| pl |
+---------------------------------+
3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。
mysql> select substring('example.com', -4);
+-------------------------------+
| substring('example.com', -4) |
+-------------------------------+
| .com |
+-------------------------------+
3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。
mysql> select substring('example.com', -4, 2);
+----------------------------------+
| substring('example.com', -4, 2) |
+----------------------------------+
| .c |
+----------------------------------+
我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。
4. 字符串截取:substring_index(str,delim,count)
4.1 截取第二个 '.' 之前的所有字符。
mysql> select substring_index('www.example.com', '.', 2);
+------------------------------------------------+
| substring_index('www.example.com', '.', 2) |
+------------------------------------------------+
| www |
+------------------------------------------------+
4.2 截取第二个 '.' (倒数)之后的所有字符。
mysql> select substring_index('www.example.com', '.', -2);
+-------------------------------------------------+
| substring_index('www.example.com', '.', -2) |
+-------------------------------------------------+
| com.cn |
+-------------------------------------------------+
4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串
mysql> select substring_index('www.example.com', '.coc', 1);
+---------------------------------------------------+
| substring_index('www.example.com', '.coc', 1) |
+---------------------------------------------------+
| www.example.com |
+---------------------------------------------------+
MySQL字符串函数substring:字符串截取的更多相关文章
- JAVA中字符串函数subString的用法小结
本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...
- mssql 系统函数-字符串函数专题--字符串函数大全
mssql 系统函数 字符串函数 substring 功能简介 mssql 系统函数 字符串函数 stuff 功能简介 mssql 系统函数 字符串函数 str 功能简介 mssql 系统函数 字符串 ...
- MySQL字符串函数:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- MySQL 字符串函数:字符串截取
1.left(name,4)截取左边的4个字符 列: ,) 年 结果:2018 2.right(name,2)截取右边的2个字符 ,) 月份 结果:09 3.SUBSTRING(name,5,3) 截 ...
- jstl 处理字符串函数 substring spli等
在jstl中的fn标签也是我们在网页设计中经常要用到的很关键的标签,在使用的时候要先加上头 <%@ taglib uri="http://java.sun.com/jsp/jstl/f ...
- MySQL通过函数获取字符串汉字拼音首字母大写字符串
DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARC ...
- MySQL replace函数替换字符串语句的用法(mysql字符串替换)
MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪. 最近在研究CMS,在数据转换的时候需要用 ...
- [原创]Delphi 字符串函数(字符串判断 TryStrToFloat 、TryStrToInt、TryStrToInt64、TryStrToBool、TryStrToCurr、TryStrToDate、TryStrToTime、TryStrToDateTime)
引用单元SysUtils TryStrToFloat .TryStrToInt.TryStrToInt64.TryStrToBool.TryStrToCurr.TryStrToDate.TryStrT ...
- 【SQL】MySQL内置函数中的字符串函数和日期时间函数
字符串函数 --拼接字符串组成新的字符串 Select concat(‘A’,’B’); --返回字符串长度 Select length(“CAT”) --返回子字符串在字符串中首次出现的位置,没有返 ...
随机推荐
- GridView详细介绍
GridView控件的属性 表10.6 GridView控件的行为属性属性描述AllowPaging指示该控件是否支持分页.AllowSorting指示该控件是否支持排序.AutoGenerateCo ...
- intellij中不能导入jar包
①选中项目点击右键,选择open modual settings(或者直接按F4): ②在弹出的窗口左端选择Libraries: ③点击左边加号,选择From Maven Repository; ④将 ...
- Struts2框架深入详解版
一.认识Struts2 1. 什么是Web框架? 1.1 模型1 1.2 模型2 和MVC 1.3 Web框架的诞生 2. Struts1 到Struts2 2.1 其他 Web框架 2.2 ...
- 对于System.exit(0)和System.exit(1)的一般理解
public static void exit(int status) 终止当前正在运行的 Java 虚拟机.参数用作状态码:根据惯例,非 0 的状态码表示异常终止. 该方法调用 Runtime 类中 ...
- 菜鸟快飞之JavaScript对象、原型、继承(二)
上一节写了创建对象的三种方法,而其中通过函数创建对象的方式又有三种模式,分别是工厂模式.构造函数模式.原型模式.而这三种模式最常用的则是原型模式.还是上栗子: 工厂模式: function Fun1( ...
- 【centos7】添加开机启动服务/脚本
一.添加开机自启服务 在centos7中添加开机自启服务非常方便,只需要两条命令(以Jenkins为例): systemctl enable jenkins.service #设置jenkins服务为 ...
- Sencha, the nightmare!
基础 创建一个应用程序 sencha -sdk /path/to/sdk generate app %name% /path/to/app 跑起来 cd /path/to/app sencha app ...
- When I see you again(加密原理介绍,代码实现DES、AES、RSA、Base64、MD5)
关于网络安全的数据加密部分,本来打算总结一篇博客搞定,没想到东西太多,这已是第三篇了,而且这篇写了多次,熬了多次夜,真是again and again.起个名字:数据加密三部曲,前两部链接如下: 整体 ...
- MS SQLServer 2008数据库处于SUSPECT情况下的处理
做任何恢复操作之前,请先备份.mdf, .ndf和.ldf文件. use master go --将处于suspect状态下的数据库设置为紧急状态 alter database <Databas ...
- redis数据结构存储SDS设计细节(redis的设计与实现笔记)
redis虽说是用C语言开发的,但是redis考虑了性能.安全性.效率性.功能等要,redis底层存储字符串实现,自己实现了名为简单动态字符串(Simple dynamic string)简称SDS的 ...