<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>
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341