<html> <body> <h2>MySQL常用字符函数简介</h2> <table> <tr> <td>CONCAT(S1,S2...Sn)</td> <td>连接S1,S2...Sn为一个字符串</td> </tr> </table> <p > concat函数,把传入的参数连接成为一个字符串。<br/> 例如:<br/> 把“aaa”、“bbb”、"ccc"3个字符串连接成一个字符串,“aaabbbccc”.另外任何与NULL进行连接的结果都将是NULL.<br/><br/> >SELECT concat('aaa','bbb','ccc'),concat('aaa',NULL); <pre> MysqL> SELECT concat('aaa','bbb','ccc'),concat('aaa',NULL); +---------------------------+--------------------+ | concat('aaa','bbb','ccc') | concat('aaa',NULL) | +---------------------------+--------------------+ | aaabbbccc | NULL | +---------------------------+--------------------+ 1 row in set (0.00 sec) </pre> </p> <br/><br/> <table> <td>INSERT(str,x,y,instr)</td> <td>将字符串str从第x位置开始,y个字符长的子串替换为字符串</td> </table> <p > insert(str,x,y,instr)函数:将字符串str从第x位置开始,y个字符长的子串替换成"me".<br/><br/<br/> <pre> MysqL> SELECT insert('beijingaiNI',10,2,'WO'); +---------------------------------+ | insert('beijingaiNI',10,2,'WO') | +---------------------------------+ | beijingaiWO | +---------------------------------+ 1 row in set (0.00 sec) </pre> </p> <br/><br/> <table> <td>LOWER(str);UPPER(str)</td> <td>将字符串str中所有字符变为小写或者大写</td> </table> <p > LOWER(str)和UPPER(str)函数:把字符串转换成小写或大写<br/> 在字符串比较中,通常要将比较的字符串全部转换为大写或者小写,如下例所示:<br/> <pre> MysqL> SELECT lower('WOAINI'),upper('woxihuanni'); +-----------------+---------------------+ | lower('WOAINI') | upper('woxihuanni') | +-----------------+---------------------+ | woaini | WOXIHUANNI | +-----------------+---------------------+ 1 row in set (0.00 sec) </pre> </p> <br/><br/> <table> <td>LEFT(str,x);RIGHT(str,x)</td> <td>返回字符串最左或最右边的x个字符串</td> </table> <p > LEFT(str,x)和RIGHT(str,x)函数:分别返回字符串最左边和最右边的x个字符,如果第二个参数是NULL,那么将不返回任何字符串。<br/> 下例所示:<br/> <pre> MysqL> SELECT left('beijing',3); +-------------------+ | left('beijing',3) | +-------------------+ | bei | +-------------------+ 1 row in set (0.01 sec) MysqL> SELECT left('beijing',NULL); +----------------------+ | left('beijing',NULL) | +----------------------+ | NULL | +----------------------+ 1 row in set (0.00 sec) </pre> </p><br/><br/> <table> <td>LPAD(str,n,pad);RPAD(str,n,pad)</td> <td>用字符串pad对str最左边或最右边进行填充,直到长度为n个字符长度(n要大于str的长度,否则就不是填充,变成截取了。)</td> </table> <p > LPAD(str,n,pad)和RPAD(str,n,pad)函数:用字符串pad对str最左边和最右边进行填充,直到长度为n个字符串。 <pre> MysqL> SELECT lpad('beijing',10,'123'); +--------------------------+ | lpad('beijing',10,'123') | +--------------------------+ | 123beijing | +--------------------------+ 1 row in set (0.00 sec) MysqL> SELECT rpad('beijing',10,'123'); +--------------------------+ | rpad('beijing',10,'123') | +--------------------------+ | beijing123 | +--------------------------+ 1 row in set (0.00 sec) </pre> </p> <br/><br/> <table> <td>LTRIM(str);RTRIM(str)</td> <td>去掉字符串str左侧和右侧的空格</td> </table> <p > LTRIM(str)和RTRIM(str)函数:去掉字符串str左侧和右侧的空格。<tr/> <pre> MysqL> SELECT ltrim(' |wo|'),rtrim('|ni| '); +-----------------+------------------+ | ltrim(' |wo|') | rtrim('|ni| ') | +-----------------+------------------+ | |wo| | |ni| | +-----------------+------------------+ 1 row in set (0.00 sec) </pre> </p><tr/><tr/> <table> <td>REPEAT(str,x)</td> <td>返回str重复x次</td> </table> <p > REPEAT(str,x)函数:返回str重复x次的结果。<br/> <pre> MysqL> SELECT repeat('beijing',3); +-----------------------+ | repeat('beijing',3) | +-----------------------+ | beijingbeijingbeijing | +-----------------------+ 1 row in set (0.00 sec) </pre> </p><tr/><tr/> <table> <td>REPLACE(str,a,b)</td> <td>用字符串b替换字符串str中所有出现的字符串a</td> </table> <p > REPLACE(str,a,b)函数:用字符串b替换字符串str中所有出现的字符串a. </p> <pre> MysqL> SELECT replace('beijing','bei','nan'); +--------------------------------+ | replace('beijing','bei','nan') | +--------------------------------+ | nanjing | +--------------------------------+ 1 row in set (0.00 sec) </pre><br/><br/> <table> <td>STRCMP(s1,s2)</td> <td>比较字符串s1和s2</td> </table> <p > STRCMP(s1,s2)函数:比较字符串s1和s2的ASCII码值的大小。<br/> 如果s1比s2小,那么就返回-1,相等返回0,大于返回1. <pre> MysqL> SELECT strcmp('a','b'),strcmp('b','b'),strcmp('c','b'); +-----------------+-----------------+-----------------+ | strcmp('a','b') | strcmp('b','b') | strcmp('c','b') | +-----------------+-----------------+-----------------+ | -1 | 0 | 1 | +-----------------+-----------------+-----------------+ 1 row in set (0.00 sec) </pre> </p><br/><br/> <table> <td>TRIM(str)</td> <td>去掉字符串行尾和行头的空格</td> </table> <p > TRIM(str)函数:去掉目标字符串的开头和结尾的空格。<br/> <pre> MysqL> SELECT trim(' ab '); +----------------+ | trim(' ab ') | +----------------+ | ab | +----------------+ 1 row in set (0.00 sec) </pre> </p><br><br/> <table> <td>SUBSTRING(str,x,y)</td> <td>返回从字符串str中的第x位置起y个字符长度的字串。</td> </table> <p > SUBSTRING(str,x,y)函数:返回从字符串str中的第x位置起y个字符长度的字串。 <pre> MysqL> SELECT substring('beijing2017',8,4); +------------------------------+ | substring('beijing2017',8,4) | +------------------------------+ | 2017 | +------------------------------+ 1 row in set (0.00 sec) </pre> </p> </body> </html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。