目录
MySQL 字符串截取函数:
left()、right()、substring()、substring_index()
。
还有 mid()、substr()
。其中,mid()、substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。
1、left(str, length):从字符串的左边取 length 长度的字符串
例子:
select left('example.com', 3);
结果为:
exa
2、right(str, length):从字符串的右边取 length 长度的字符串
例子:
select right('example.com', 3);
结果为:
com
3、substring(str, pos)、substring(str, pos, len)
3.1、从字符串的第 4 个字符位置开始取,直到结束
select substring('example.com', 4);
结果为:
ple.com
3.2、从字符串的第 4 个字符位置开始取,只取 2 个字符
select substring('example.com', 4, 2);
结果为:
pl
3.3、从字符串的倒数第 4 个字符位置开始取,直到结束
select substring('example.com', -4);
结果为:
.com
3.4、从字符串的倒数第 4 个字符位置开始取,只取 2 个字符
select substring('example.com', -4, 2);
结果为:
.c
转载于:https://blog.csdn.net/weixin_42303389/article/details/112826502