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:字符串截取的更多相关文章

  1. JAVA中字符串函数subString的用法小结

    本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...

  2. mssql 系统函数-字符串函数专题--字符串函数大全

    mssql 系统函数 字符串函数 substring 功能简介 mssql 系统函数 字符串函数 stuff 功能简介 mssql 系统函数 字符串函数 str 功能简介 mssql 系统函数 字符串 ...

  3. MySQL字符串函数:字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

  4. MySQL 字符串函数:字符串截取

    1.left(name,4)截取左边的4个字符 列: ,) 年 结果:2018 2.right(name,2)截取右边的2个字符 ,) 月份 结果:09 3.SUBSTRING(name,5,3) 截 ...

  5. jstl 处理字符串函数 substring spli等

    在jstl中的fn标签也是我们在网页设计中经常要用到的很关键的标签,在使用的时候要先加上头 <%@ taglib uri="http://java.sun.com/jsp/jstl/f ...

  6. MySQL通过函数获取字符串汉字拼音首字母大写字符串

    DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARC ...

  7. MySQL replace函数替换字符串语句的用法(mysql字符串替换)

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪. 最近在研究CMS,在数据转换的时候需要用 ...

  8. [原创]Delphi 字符串函数(字符串判断 TryStrToFloat 、TryStrToInt、TryStrToInt64、TryStrToBool、TryStrToCurr、TryStrToDate、TryStrToTime、TryStrToDateTime)

    引用单元SysUtils TryStrToFloat .TryStrToInt.TryStrToInt64.TryStrToBool.TryStrToCurr.TryStrToDate.TryStrT ...

  9. 【SQL】MySQL内置函数中的字符串函数和日期时间函数

    字符串函数 --拼接字符串组成新的字符串 Select concat(‘A’,’B’); --返回字符串长度 Select length(“CAT”) --返回子字符串在字符串中首次出现的位置,没有返 ...

随机推荐

  1. USB设备(移动硬盘、鼠标)掉电掉驱动的两种解决方案

    症状: 当你发现"移动硬盘图标"经常无故消失,又自己出现时. 你可以把这个现象称之为"掉电" or "掉驱动". 遇到这种情况,相当不爽. ...

  2. ASP.NET + EF + SQL Server搭建个人博客系统新手系列(一):界面展示

    第一次写博客,可能有些地方描述的不准确,还请大家将就.本人大四学生,学了半年C#,半年.net,但是很遗憾,学完之后只会写个hello word.老师教的过程中总是会套用一些模板,特别是后台,完全封装 ...

  3. android

    配置Activity 的启动模式: 在 AndroidManifest.xml 中配置: <activity android:name=".MainActivity" and ...

  4. JavaWeb_day06_Filter过滤器

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day06 request 对象常用方法 respon ...

  5. TileJSON

    TileJSON TileJSON is an open standard for representing map metadata. License The text of this specif ...

  6. 缓冲区溢出利用——捕获eip的傻瓜式指南

    [译文] 摘要:为一个简单的有漏洞程序写一个简单的缓冲区溢出EXP,聚焦于遇到的问题和关键性的教训,提供详细而彻底的描述 内容表:1. I pity the fool, who can't smash ...

  7. 使用Kotlin对ViewGroup的视图进行函数使操作

    原文标题:Functional operations over Views in ViewGroup using Kotlin 原文链接:http://antonioleiva.com/functio ...

  8. iOS 获取当前点击的坐标

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSSet *allTouch ...

  9. Appfuse:添加自定义页面组件

    我之前是做ASP.NET的,碰到被多个页面都使用的类似组件后,就想着采用ascx(用户自定义组件)来解决,那做Java我也想用这种方案. 我要做的效果如下: 实现方案:tag方式(自定义标签) 1. ...

  10. sipex3232上电发送输入端拉低问题

    之前没怎么注意,在使用下载盒通过isp下载stm32时,由于是通过stm32板子给下载盒供电,所以是我的stm32板子和下载盒同时上电.这样总是不能够进入下载模式,后来对下载盒进行仿真,发现在下载发送 ...