MySQL中的substr()函数

14秒前 689阅读

截取函数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);

MySQL中的substr()函数

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);

MySQL中的substr()函数

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);

MySQL中的substr()函数

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()函数

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]