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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM