• mysql 字符串截取


    转载自:https://www.cnblogs.com/zdz8207/p/3765073.html
    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);

    1. 字符串截取:substring(str, pos); substring(str, pos, len)

    3.1 从字符串的第 4 个字符位置开始取,直到结束。

    mysql> select substring(‘example.com’, 4);
    ±-----------------------------+
    | substring(‘example.com’, 4) |
    ±-----------------------------+
    | mple.com |
    ±-----------------------------+
    3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。

    mysql> select substring(‘example.com’, 4, 2);
    ±--------------------------------+
    | substring(‘example.com’, 4, 2) |
    ±--------------------------------+
    | mp |
    ±--------------------------------+
    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 不能取负值。

    1. 字符串截取:substring_index(str,delim,count)

    4.1 截取第二个 ‘.’ 之前的所有字符。

    mysql> select substring_index(‘www.example.com’, ‘.’, 2);
    ±-----------------------------------------------+
    | substring_index(‘www.example.com’, ‘.’, 2) |
    ±-----------------------------------------------+
    | www.example |
    ±-----------------------------------------------+
    4.2 截取第二个 ‘.’ (倒数)之后的所有字符。

    mysql> select substring_index(‘www.example.com’, ‘.’, -2);
    ±------------------------------------------------+
    | substring_index(‘www.example.com’, ‘.’, -2) |
    ±------------------------------------------------+
    | example.com |
    ±------------------------------------------------+
    4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串

    mysql> select substring_index(‘www.example.com’, ‘.coc’, 1);
    ±--------------------------------------------------+
    | substring_index(‘www.example.com’, ‘.coc’, 1) |
    ±--------------------------------------------------+
    | www.example.com |
    ±--------------------------------------------------+

  • 相关阅读:
    MDM数据质量应用说明
    http协议的请求方法 —— GET、POST方法及其区别
    AI作画飞入寻常百姓家——stable diffusion初体验
    SpringBoot整合Junit
    【Python模块】日期时间
    无处不在的边缘网络感知
    杨辉三角(Java实现)
    Android Framework如何从萌白到封神学习?
    从零打造“乞丐版” React(一)——从命令式编程到声明式编程
    PostgreSQL DBA快速入门-通过源码编译安装
  • 原文地址:https://blog.csdn.net/qq_50990903/article/details/126121942