mysql 字符串 拼接 截取 替换

一. 字符串拼接

concat('asdf',str);  -- asdfhello

二、字符串截取

从左开始截取字符串

 left(str, length) --说明:left(被截取字段,截取长度)    select left('hello',3);  --hel

从右开始截取字符串

right(str,length) --说明:right(被截取字段,截取长度) 例:  select right('hello',2);  --lo

按长度截取字符串

substring(str, pos) 
substring(str, pos, length)  --说明:substring(被截取字段,从第几位开始截取) substring(被截取字段,从第几位开始截取,截取长度)
substring(str, pos)   --pos从1 开始  当pos为0位null
substring(str, pos, length)  --说明:substring(被截取字段,从第几位开始截取)  substring(被截取字段,从第几位开始截取,截取长度)
SUBSTRING(string FROM position FOR length);
select substring('hello,world',0);  --
select substring('hello,world',2);  --ello,world
select substring('hello,world',2,2); --el
SELECT substring('Hello World',-11); --Hello World
SELECT substring('Hello World',-4); --orld

三. 字符串替换

replace(str,original,replace)  --说明:replace(字符串,被替换的字符串,替换成的字符串) 例: 
select  replace('hello world','he','zou'); zoullo world   

substr()函数


1、substr(str,pos);

SELECT SUBSTR('2018-08-17',6);

2、substr(str from pos);

SELECT SUBSTR('2018-08-17' FROM 6);

3、substr(str,pos,len);//str:字符串,pos:起始位置,len:截断长度

SELECT SUBSTR('2018-08-17',6,7);

4、substr(str from pos len);

SELECT SUBSTR('2018-08-17' FROM 6 FOR 7);

与MySQL一样,position>0和position<0时是一样的效果,参照上面的下标对应即可,不同的是,position=0和position=1的效果是一样的。

 

SELECT SUBSTR('Hello World',0) FROM DUAL;
SELECT SUBSTR('Hello World',1) FROM DUAL;
SELECT SUBSTR('Hello World',-11) FROM DUAL;
Hello World
SELECT SUBSTR('Hello World',1,5) FROM DUAL;
SELECT SUBSTR('Hello World',6,20) FROM DUAL;
Hello
World

 

原文地址:https://www.cnblogs.com/zouhong/p/13670248.html