MySQL中substr()函数的使用示例

2024-04-29 12:07:36 来源/作者: 这里教程网 /

截取函数substr()方法以及参数详解

1、substr(str, position)   从position截取到字符串末尾

str可以是字符串、函数、SQL查询语句

position代表起始位置,索引位置从1开始

select substr(now(), 6); select substr('2023-10-25', 6); select substr((select fieldName from tableName where condition), 1);

2、substr(str from position)  从position截取到字符串末尾

和1的操作类似,和上面1的操作对比可以发现只是把括号中的逗号换成from关键字

select substr(now() from 6); select substr('2023-10-25' from 6); select substr((select fieldName from tableName where condition) from 1);

3、substr(str, position, length)  从position截取长度为length的字符串

str可以是字符串、函数、SQL查询语句

position代表起始位置

length代表截取的字符串长度

select substr(now(), 1, 4); select substr('2024-01-01', 1, 4); select substr((select fieldName from tableName where condition), 1, 4);

4、substr(str from position for length)  从position截取长度为length的字符串

和3的操作类似,和上面3的操作对比可以发现只是把括号中的第一个逗号换成from关键字,

第二个逗号换成了for关键字

select substr(now() from 6 for 5); select substr('2023-10-01' from 6 for 5); select substr((select fieldName from tableName where condition) from 6 for 5);

到此这篇关于MySQL中substr()函数的使用示例的文章就介绍到这了,