MySQL 字符串截取函数
- MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。
- 1. 字符串截取:left(str, length)
- mysql> select left("sqlstudy.com", 3);
- +-------------------------+
- | left("sqlstudy.com", 3) |
- +-------------------------+
- | sql |
- +-------------------------+
- 2. 字符串截取:right(str, length)
- mysql> select right("sqlstudy.com", 3);
- +--------------------------+
- | right("sqlstudy.com", 3) |
- +--------------------------+
- | com |
- +--------------------------+
- 3. 字符串截取:substring(str, pos); substring(str, pos, len)
- 3.1 从字符串的第 4 个字符位置开始取,直到结束。
- mysql> select substring("sqlstudy.com", 4);
- +------------------------------+
- | substring("sqlstudy.com", 4) |
- +------------------------------+
- | study.com |
- +------------------------------+
- 3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。
- mysql> select substring("sqlstudy.com", 4, 2);
- +---------------------------------+
- | substring("sqlstudy.com", 4, 2) |
- +---------------------------------+
- | st |
- +---------------------------------+
- 3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。
- mysql> select substring("sqlstudy.com", -4);
- +-------------------------------+
- | substring("sqlstudy.com", -4) |
- +-------------------------------+
- | .com |
- +-------------------------------+
- 3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。
- mysql> select substring("sqlstudy.com", -4, 2);
- +----------------------------------+
- | substring("sqlstudy.com", -4, 2) |
- +----------------------------------+
- | .c |
- +----------------------------------+
- 我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。
- 4. 字符串截取:substring_index(str,delim,count)
- 4.1 截取第二个 "." 之前的所有字符。
- mysql> select substring_index("www.sqlstudy.com.cn", ".", 2);
- +------------------------------------------------+
- | substring_index("www.sqlstudy.com.cn", ".", 2) |
- +------------------------------------------------+
- | www.sqlstudy |
- +------------------------------------------------+
- 4.2 截取第二个 "." (倒数)之后的所有字符。
- mysql> select substring_index("www.sqlstudy.com.cn", ".", -2);
- +-------------------------------------------------+
- | substring_index("www.sqlstudy.com.cn", ".", -2) |
- +-------------------------------------------------+
- | com.cn |
- +-------------------------------------------------+
- 4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串
- mysql> select substring_index("www.sqlstudy.com.cn", ".coc", 1);
- +---------------------------------------------------+
- | substring_index("www.sqlstudy.com.cn", ".coc", 1) |
- +---------------------------------------------------+
- | www.sqlstudy.com.cn |
- +---------------------------------------------------+
- 4.4 截取一个表某个字段数据的中间值 如该字段数据为 1,2,3
- mysql> select substring_index(substring_index(该字段, ",", 2) , ",", -1) from 表名;
- +--------------------------------------------------------------+
- | substring_index(substring_index(该字段, ",", 2); , ",", -1)|
- +--------------------------------------------------------------+
- | 2 |
- +--------------------------------------------------------------+
- ASCII(str)
- 返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL。
- Java代码
- 如:select ASCII("2");select ASCII(2); 结果都为2的Ascii码值 50
- 如:select ASCII("2");select ASCII(2); 结果都为2的Ascii码值 50
- CHAR(N,...)
- CHAR()将参数解释为整数并且返回由这些整数的ASCII代码字符组成的一个字符串。NULL值被跳过。
- Java代码
- select CHAR(77,121,83,81,NULL); 结果为 MySQ ;
- select CHAR(77,121,83,81,NULL); 结果为 MySQ ;
- [color=darkred]CONCAT(str1,str2,...)
- 返回来自于参数连结的字符串。如果任何参数是NULL,返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。
- Java代码
- select CONCAT("My", "S", "QL"); //MySQL
- select CONCAT(12.3); //"12.3"
- select CONCAT("My", "S", "QL"); //MySQL
- select CONCAT(12.3); //"12.3"
- LENGTH(str)
- 计算字符串长度 :select length("text") ; //4
- LOCATE(substr,str)
- POSITION(substr IN str)
- 返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0.
- Java代码
- 1.mysql> select LOCATE("bar", "foobarbar");
- 2. -> 4
- 3.mysql> select LOCATE("xbar", "foobar");
- 4. -> 0
- mysql> select LOCATE("bar", "foobarbar");
- -> 4
- mysql> select LOCATE("xbar", "foobar");
- -> 0该函数是多字节可靠的
- LOCATE(substr,str,pos)
- 返回子串substr在字符串str第一个出现的位置,从位置pos开始。如果substr不是在str里面,返回0。
- Java代码
- 1.mysql> select LOCATE("bar", "foobarbar",5);
- 2. -> 7
- mysql> select LOCATE("bar", "foobarbar",5);
- -> 7
- INSTR(str,substr)
- 返回子串substr在字符串str中的第一个出现的位置。这与有2个参数形式的LOCATE()相同,除了参数被颠倒。
- Java代码
- 1.mysql> select INSTR("foobarbar", "bar");
- 2. -> 4
- 3.mysql> select INSTR("xbar", "foobar");
- 4. -> 0
- mysql> select INSTR("foobarbar", "bar");
- -> 4
- mysql> select INSTR("xbar", "foobar");
- -> 0这函数是多字节可靠的。
- LPAD(str,len,padstr)
- 返回字符串str,左面用字符串padstr填补直到str是len个字符长。
- Java代码
- 1.<PRE class=java name="code">mysql> select LPAD("hi",4,"??");
- 2. -> "??hi"
- 3. [color=darkred]RPAD(str,len,padstr)[/color] </PRE>返回字符串str,右面用字符串padstr填补直到str是len个字符长。
- Java代码 mysql> select LPAD("hi",4,"??"); -> "??hi"
- mysql> select LPAD("hi",4,"??");
- -> "??hi"
- [color=darkred]RPAD(str,len,padstr)[/color]
- 返回字符串str,右面用字符串padstr填补直到str是len个字符长。
- mysql> select RPAD("hi",5,"?");
- -> "hi???"
- LEFT(str,len)
- 返回字符串str的最左面len个字符。
- Java代码
- 1.mysql> select LEFT("foobarbar", 5);
- 2. -> "fooba"该函数是多字节可靠的。
- mysql> select LEFT("foobarbar", 5);
- -> "fooba"该函数是多字节可靠的。
- RIGHT(str,len)
- 返回字符串str的最右面len个字符。
- Java代码
- 1.mysql> select RIGHT("foobarbar", 4);
- 2. -> "rbar"
- mysql> select RIGHT("foobarbar", 4);
- -> "rbar"
- 该函数是多字节可靠的。
- SUBSTRING(str,pos)
- SUBSTRING(str FROM pos)
- 从字符串str的起始位置pos返回一个子串。
- mysql> select SUBSTRING("Quadratically",5);
- -> "ratically"
- mysql> select SUBSTRING("foobarbar" FROM 4);
- -> "barbar"
- 该函数是多字节可靠的。
- SUBSTRING_INDEX(str,delim,count)
- 返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符。如果count是负数,返回最后的分隔符到右边的所有字符(从右边数)。
- mysql> select SUBSTRING_INDEX("www.mysql.com", ".", 2);
- -> "www.mysql"
- mysql> select SUBSTRING_INDEX("www.mysql.com", ".", -2);
- -> "mysql.com"
- 该函数对多字节是可靠的。
- LTRIM(str)
- 返回删除了其前置空格字符的字符串str。
- mysql> select LTRIM(" barbar");
- -> "barbar"
- RTRIM(str)
- 返回删除了其拖后空格字符的字符串str。
- mysql> select RTRIM("barbar ");
- -> "barbar"
- 该函数对多字节是可靠的。
- TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
- 返回字符串str,其所有remstr前缀或后缀被删除了。如果没有修饰符BOTH、LEADING或TRAILING给出,BOTH被假定。如果remstr没被指定,空格被删除。
- mysql> select TRIM(" bar ");
- -> "bar"
- mysql> select TRIM(LEADING "x" FROM "xxxbarxxx");
- -> "barxxx"
- mysql> select TRIM(BOTH "x" FROM "xxxbarxxx");
- -> "bar"
- mysql> select TRIM(TRAILING "xyz" FROM "barxxyz");
- -> "barx"
- 该函数对多字节是可靠的。
- SOUNDEX(str)
- 返回str的一个同音字符串。听起来“大致相同”的2个字符串应该有相同的同音字符串。一个“标准”的同音字符串长是4个字符,但是SOUNDEX()函数返回一个任意长的字符串。你可以在结果上使用SUBSTRING()得到一个“标准”的 同音串。所有非数字字母字符在给定的字符串中被忽略。所有在A-Z之外的字符国际字母被当作元音。
- mysql> select SOUNDEX("Hello");
- -> "H400"
- mysql> select SOUNDEX("Quadratically");
- -> "Q36324"
- SPACE(N)
- 返回由N个空格字符组成的一个字符串。
- mysql> select SPACE(6);
- -> " "
- REPLACE(str,from_str,to_str)
- 返回字符串str,其字符串from_str的所有出现由字符串to_str代替。
- mysql> select REPLACE("www.mysql.com", "w", "Ww");
- -> "WwWwWw.mysql.com"
- 该函数对多字节是可靠的。
- REPEAT(str,count)
- 返回由重复countTimes次的字符串str组成的一个字符串。如果count <= 0,返回一个空字符串。如果str或count是NULL,返回NULL。
- mysql> select REPEAT("MySQL", 3);
- -> "MySQLMySQLMySQL"
- REVERSE(str)
- 返回颠倒字符顺序的字符串str。
- mysql> select REVERSE("abc");
- -> "cba"
- 上一篇: MySQL——字符串函数
- 下一篇: mysql字符串函数练习
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
copyright © 2008-2019 亿联网络 版权所有 备案号:粤ICP备14031511号-2