第一种情况:利用指定字符串截取对应其他字符串
-
mysql: substring_index(str,delim,count),其中str为被截取的字符串;delim为指定字符串;count计数,如果为整数,则从左边开始数,如果为负数,则从右边开始数。
例子: 截取固定地址生成规则eccp后且.html前的字符串: url=http://buy.ccb.html/firstchannel/secondchannel/eccp_AB0123.html?orderid=12345&password=12345,
匹配规则为:
select substring_index (substring_index (url , ‘eccp_’ , -1) , ‘.html’ , 1)
结果为:AB0123 -
postgresql:split_part(str, delim, integer ), 其中str为被截取的字符串;delim为指定字符串;integer为第几部分。
例子: 截取固定地址生成规则eccp后且.html前的字符串: url=http://buy.ccb.html/firstchannel/secondchannel/eccp_AB0123.html?orderid=12345&password=12345,
匹配规则为:
select split_part (split_part (url , ‘eccp_’ , 2) , ‘.html’ , 1)
结果为:AB0123
第二种情况:直接截取固定长度字符串
mysql 与postgresql相同: substr(str,pos,len) ,其中str为被截取的字符串;pos为截取的位置,第一个字符串为1;len表示截取的长度。
例子: select SUBSTR(‘123456’,1,3)
结果为: 123
本文介绍在MySQL和PostgreSQL中如何使用substring_index和split_part函数精确截取字符串,以及substr函数截取固定长度字符串的方法。

2420

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



