截断字符串
myql 中主要的字符串截取函数包括left(), right(), substring(), substring_index():
left函数:LEFT(str,len) 从字符串左边开始截断字符串
函数说明:str 需要截断的字符串 len 截取长度
eg: select LEFT("hello mysql",5)
:
right函数:RIGHT(str,len) 从字符串右边开始截断字符串
函数说明:str 需要截断的字符串,len 截取长度
eg: select RIGHT("hello mysql",5) ;

substring函数:
- substring(str, pos),即:substring(被截取字符串, 从第几位开始截取)
- substring(str, pos, length),即:substring(被截取字符串,从第几位开始截取,截取长度)
eg:
1、select SUBSTRING("hello mysql",5) ;

2、select SUBSTRING("hello mysql",2,5) ;

substring_index函数:
函数说明:substring_index(str, delim, count),即:substring_index(被截取字符串,分隔符,计数)
其中count 可为负数 ,表示从右边开始取值;
eg:
1、select substring_index("hello mysql test",' ',2);

2、select substring_index("hello mysql test",' ',-1);

本文详细介绍了MySQL中四种常用的字符串截断函数:left(), right(), substring(), substring_index()的使用方法及实例。通过这些函数,可以实现从字符串的左侧、右侧截取,以及指定位置开始截取,并支持按分隔符进行截断。

被折叠的 条评论
为什么被折叠?



