SQL之操作字符串函数CONCAT、SUBSTR、SUBSTRING、SUBSTRING_INDEX、LEFT、RIGHT

以MySQL为例

CONCAT()

    CONCAT(str1,str2,...) 返回字符串参数连接后的结果。参数是非二进制字符串(参数个数不固定),返回时非二进制字符串。

table

- str1 str2
1 my sql
SELECT CONCAT(t.str1,t.str2) from table
>mysql

SUBSTR()//SUBSTRING()

    两个函数都是截取字符串

SUBSTR(str,pos)//SUBSTR(str FROM pos)

    str从pos位置开始截取到最后
    注:pos第一个下标是1!!!

table(表名)

- str
1 demo.com.cn
SELECT SUBSTR(t.str,1)FROM `table` t;
>demo.com.cn

SELECT SUBSTR(t.str,3)FROM `table` t;
>mo.com.cn

SUBSTR(str,pos,len)//SUBSTR(str FROM pos FOR len)

    str从pos位置开始截取到len位字符
    注:pos第一个下标是1!!!
SELECT SUBSTR(t.str,1,8)FROM `table` t;
>demo.com

SUBSTRING_INDEX()

    按某些字符的下标来截取字符串

SUBSTRING_INDEX(str,delim,count)

    从str中找到delim字符串按下标获取字符串

table(表名)

- str
1 demo.com.cn
SELECT SUBSTRING_INDEX(t.str,'.',1) FROM `table` t;
>demo
SELECT SUBSTRING_INDEX(t.str,'.',-1) FROM `table` t;
>cn
SELECT SUBSTRING_INDEX(t.str,'.',2) FROM `table` t;
>demo.com

LEFT()

    从左边截取字符串

LEFT(str,len)

    从str左边截取len长度字符串
SELECT LEFT(t.str,4) FROM `table` t;
>demo

RIGHT()

    从str右边截取len长度字符串

RIGHT(str,len)

    从str右边截取len长度字符串
SELECT RIGHT(t.str,6)FROM `table` t;
>com.cn

你可能感兴趣的:(SQL)