您好,登錄后才能下訂單哦!
<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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。