加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL的字符串截取函数

发布时间:2020-12-12 00:02:09 所属栏目:MySql教程 来源:网络整理
导读:MySQL字符串截取函数有:left(),right(),substring(),substring_index(),还有mid() ,subset()。但mid() ,subset()和substring()功能一样。所以会用substring()就行了。 为方便起见,所以在本次MySQL连接中设置了一个用户自定义变量,字符串@str,以方

MySQL字符串截取函数有:left(),right(),substring(),substring_index(),还有mid() ,subset()。但mid() ,subset()和substring()功能一样。所以会用substring()就行了。

为方便起见,所以在本次MySQL连接中设置了一个用户自定义变量,字符串@str,以方便下面多次对其的操作。

1、自定义变量设定的sql:

2、left()

3、right()

4、substring()

-- substring(str FROM pos)
-- 从字符串的第4个位置,到结尾为止
select substring(@str from 4) -- 结果:.hao123.com.cn

-- substring(str,pos,len)
-- 从字符串的第1个位置,截取长度为4
select substring(@str,1,4) -- 结果:www.

-- substring(str,pos)
-- 从字符串的第4个位置,到结尾为止
select substring(@str,4) -- 结果:.hao123.com.cn

-- substring(str FROM pos)
-- 从字符串倒数第4个为止开始截取,到字符串结束
select substring(@str from -4) -- 结果:m.cn

-- substring(str,len)
-- 从字符串倒数第5个位置开始截取,截取长度为3
select substring(@str,-5,3) -- 结果:om.

-- substring(str,pos)
-- 从字符串倒数第4个为止开始截取,到字符串结束
select substring(@str,-4) -- 结果:m.cn

5、substring_index()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读