首页 > 数据库 > MySQL > 正文

MySQL常用字符函数介绍

2024-07-24 12:35:01
字体:
来源:转载
供稿:网友
  <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>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>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>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>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/>
 
  </body>
  </html>

(编辑:武林网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表