mysql截取字符串的函数

1. LEFT(str,len)

  从左边开始截取,str:被截取字符串;len:截取长度

	select left('www.baidu.com', 3);
	# www

2. RIGHT(str,len)

  从右边开始截取,str:被截取字符串;len:截取长度

	select right('www.baidu.com', 3);
	# com

3. SUBSTR(str, pos, len)

  str:被截取字符串;pos:截取开始位置;len:截取长度(非必填:不填截取到最大长度)

	select substr('www.baidu.com', 3);
	# w.baidu.com

4. SUBSTRING(str, pos, len)

  str:被截取字符串;pos:截取开始位置;len:截取长度(非必填:不填截取到最大长度)

	select substring('www.baidu.com', 3);
	# w.baidu.com

5. SUBSTRING_INDEX(str, delim, count)

  str:被截取字符串;delim:截取数据依据的字符;count:截取数量(count>0就从左边开始取,count<0就从右边开始取)

	select substring_index('www.baidu.com', '.', 1);
	# www
	select substring_index('www.baidu.com', '.', -1);
	# com

6. LOCATE(substr,str)

  substr:搜索的字符;str:字符串;从 str 字符串 中获得 substr 字符 的位置(没找到返回 0)

	select LOCATE('www', 'www.baidu.com');
	# 1
	select LOCATE('cn', 'www.baidu.com');
	# 0

猜你喜欢

转载自blog.csdn.net/bai_mi_student/article/details/130603150