<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          掌握 MySQL 常用字符串函數(shù):快速提升數(shù)據(jù)庫操作效率

          共 8694字,需瀏覽 18分鐘

           ·

          2024-07-02 16:30

          在 MySQL 中,字符串函數(shù)用于處理和操作字符串?dāng)?shù)據(jù)。這些函數(shù)在查詢和數(shù)據(jù)處理過程中非常有用。以下是一些常用的 MySQL 字符串函數(shù)及其簡(jiǎn)要說明:

          一、常用函數(shù)

          1.1 ascii(str)

          返回值為字符串str的最左字符的數(shù)值,即取得最左字符的ascii碼。假如str為空字符串,則返回值為0。假如str為null,則返回值為null。

          mysql> select ascii('a');
          +--------------+
          | ascii('a') |
          +--------------+
          | 97 |
          +--------------+
          1 row in set (0.00 sec)


          1.2 char()

          將ascii碼轉(zhuǎn)換為字符。如果沒有輸入0~255之間的ascii碼值,char()返回null。

          1.3 lower()和upper()

          lower()將字符串全部轉(zhuǎn)為小寫;
          upper()將字符串全部轉(zhuǎn)為大寫。

          mysql> select lower("HELLO WORLD"), upper("hello world");
          +----------------------+----------------------+
          | lower("HELLO WORLD") | lower("hello world") |
          +----------------------+----------------------+
          | hello world | HELLO WORLD |
          +----------------------+----------------------+


          1.4 str()

          把數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字符型數(shù)據(jù)。

          str(<float_expression>[length] ]])length指定返回的字符串的長(zhǎng)度,decimal指定返回的小數(shù)位數(shù)。如果沒有指定長(zhǎng)度,缺省的length值為10,decimal缺省值為0。當(dāng)length或者decimal為負(fù)值時(shí),返回null;當(dāng)length小于小數(shù)點(diǎn)左邊(包括符號(hào)位)的位數(shù)時(shí),返回length個(gè)*;先服從length,再取decimal;當(dāng)返回的字符串位數(shù)小于length,左邊補(bǔ)足空格。

          二、去空格函數(shù)

          2.1 trim()

          去除兩邊的空格

          mysql> select trim('   bar   ');
          +-------------------+
          | trim(' bar ') |
          +-------------------+
          | bar |
          +-------------------+
          1 row in set (0.13 sec)


          指定字符串截取 左邊截取字符串

          mysql> select trim(leading 'X' from 'XXXXXtrimleadingXXXX');
          +-----------------------------------------------+
          | trim(leading 'X' from 'XXXXXtrimleadingXXXX') |
          +-----------------------------------------------+
          | trimleadingXXXX |
          +-----------------------------------------------+
          1 row in set (0.13 sec)


          指定去除字符串右邊的xyz,左邊截取字符串

          mysql> select trim(trailing 'zyz' from 'barxxyz');
          +-------------------------------------+
          | trim(trailing 'zyz' from 'barxxyz') |
          +-------------------------------------+
          | barxxyz |
          +-------------------------------------+
          1 row in set (0.12 sec)


          指定去除字符串兩邊的X

          mysql> select trim(both 'X' from 'XXXXXXXtrimbothXXXXXXX');
          +----------------------------------------------+
          | trim(both 'X' from 'XXXXXXXtrimbothXXXXXXX') |
          +----------------------------------------------+
          | trimboth |
          +----------------------------------------------+
          1 row in set (0.13 sec)


          2.2 ltrim()

          把字符串頭部的空格去掉。

          2.3 rtrim()

          把字符串尾部的空格去掉。

          三、取子串函數(shù)

          3.1 left()

          left(<character_expression>,<integer_expression>)返回character_expression左起integer_expression個(gè)字符。

          3.2 right()

          right(<character_expression>,<integer_expression>)返回character_expression右起integer_expression個(gè)字符。

          3.3 substring()

          格式:substring(s,start,length)

          從字符串s的start位置截取長(zhǎng)度為length的子字符串

          mysql> select substring("Hello world", 5);
          +-----------------------------+
          | substring("Hello world", 5) |
          +-----------------------------+
          | o world |
          +-----------------------------+
          1 row in set (0.07 sec)

          mysql> select substring("Hello world", 5, 3);
          +--------------------------------+
          | substring("Hello world", 5, 3) |
          +--------------------------------+
          | o w |
          +--------------------------------+
          1 row in set (0.07 sec)

          mysql> select substring("Hello world", -5);
          +------------------------------+
          | substring("Hello world", -5) |
          +------------------------------+
          | world |
          +------------------------------+
          1 row in set (0.08 sec)


          3.4 substring_index()

          格式:substring_index(s,delimiter,number)

          返回從字符串s的第number個(gè)出現(xiàn)的分隔符delimiter之前的子串;如果number是正數(shù),那么就是從左往右數(shù),返回第number個(gè)分隔符的左邊的全部?jī)?nèi)容;相反,如果number是負(fù)數(shù),那么就是從右邊開始數(shù),第number個(gè)分隔符右邊的所有內(nèi)容

          注意:如果number超過了實(shí)際分隔符的個(gè)數(shù),則返回實(shí)際個(gè)數(shù)的字符串

          參數(shù)說明:

          • str: 待截取的字符串,string類型

          • delimiter: 分隔符,string類型

          • number: 指定分隔符位置,int類型

          返回值:

          返回string類型字符串,如果任一輸入?yún)?shù)為null,則返回null

          -- 案例1
          select substring_index('a*b','*',1); -- a

          -- 案例2
          select substring_index('a*b','*',-1); -- b

          -- 案例3
          select substring_index(substring_index('a*b*c*d*e','*',3),'*',-1); -- c
          -- 案例3可拆解成以下兩個(gè)步驟
          select substring_index('a*b*c*d*e','*',3); -- a*b*c
          select substring_index('a*b*c','*',-1); -- c

          -- 案例4
          -- 如果任一輸入?yún)?shù)為null,則返回null
          select substring_index('https://www.google.com', null, 2);
          select substring_index('https://www.google.com', '.', null);
          select substring_index(null, '.', 2);

          -- 案例5
          -- 如果number超過了實(shí)際分隔符的個(gè)數(shù),則返回實(shí)際個(gè)數(shù)的字符串
          -- https://www.google.com
          select substring_index('https://www.google.com', '.', 4);


          四、字符串比較函數(shù)

          4.1 charindex()

          返回字符串中某個(gè)指定的子串出現(xiàn)的開始位置。

          charindex(<'substring_expression'>, )其中substring_expression是所要查找的字符表達(dá)式,expression可為字符串也可為列名表達(dá)式。如果沒有發(fā)現(xiàn)子串,則返回0值。此函數(shù)不能用于text和image數(shù)據(jù)類型。

          4.2 patindex()

          返回字符串中某個(gè)指定的子串出現(xiàn)的開始位置。

          patindex(<'%substring_expression%'>,<column_name="">)其中子串表達(dá)式前后必須有百分號(hào)“%”否則返回值為0。與charindex函數(shù)不同的是,patindex函數(shù)的子串中可以使用通配符,且此函數(shù)可用于char、varchar和text數(shù)據(jù)類型。

          五、字符串操作函數(shù)

          5.1 quotename()

          返回被特定字符括起來的字符串。quotename(<'character_expression'>[,quote_character])其中quote_character標(biāo)明括字符串所用的字符,缺省值為“[]”。

          5.2 replicate()

          返回一個(gè)重復(fù)character_expression指定次數(shù)的字符串。

          replicate(character_expression integer_expression)如果integer_expression值為負(fù)值,則返回null。

          5.3 reverse()

          格式:reverse(s) 其中s可以是字符串、常數(shù)或一個(gè)列的值。

          將指定的字符串的字符排列順序顛倒。

          mysql> select reverse("abcdef");
          +-----------------+
          | reverse("abcdef") |
          +-----------------+
          | fedcba |
          +-----------------+

          1 row in set (0.06 sec)


          5.4 replace()

          格式: replace( , , )

          用s3替換在s1中的子串s2。

          返回被替換了指定子串的字符串。

          mysql> select replace('www.mysql.com','w','W');
          +----------------------------------+
          | replace('www.mysql.com','w','W') |
          +----------------------------------+
          | WWW.mysql.com |
          +----------------------------------+

          1 row in set (0.07 sec)


          5.5 space()

          返回一個(gè)有指定長(zhǎng)度的空白字符串。

          space(<integer_expression>)如果integer_expression值為負(fù)值,則返回null。

          5.6 stuff()

          用另一子串替換字符串指定位置、長(zhǎng)度的子串。

          stuff(<character_expression1>,<start_position="">, ,<character_expression2>)

          如果起始位置為負(fù)或長(zhǎng)度值為負(fù),或者起始位置大于character_expression1的長(zhǎng)度,則返回null值。

          如果length長(zhǎng)度大于character_expression1中start_position以右的長(zhǎng)度,則character_expression1只保留首字符。

          5.7 length()

          查看字符串的長(zhǎng)度 這里的UTF8 中文是占用兩個(gè)字節(jié)

          mysql> select length("text");
          +----------------+
          | length("text") |
          +----------------+
          | 4 |
          +----------------+

          mysql> select length("你好");
          +----------------+
          | length("你好") |
          +----------------+
          | 6 |
          +----------------+
          1 row in set (0.07 sec)


          5.8 char_length()

          返回一共有多少個(gè)字

          mysql> select char_length("text");
          +---------------------+
          | char_length("text") |
          +---------------------+
          | 4 |
          +---------------------+

          mysql> select char_length("你好");
          +---------------------+
          | char_length("你好") |
          +---------------------+
          | 2 |
          +---------------------+
          1 row in set (0.07 sec)


          5.8 repeat()

          repeat(str,count) 將字符串str重復(fù)count次后返回

          mysql> select repeat('MySQL',3);
          +--------------------+
          | repeat('MySQL',3) |
          +--------------------+
          | MySQLMySQLMySQL |
          +--------------------+
          1 row in set (0.07 sec)


          5.9 reverse()

          將字符串反轉(zhuǎn)后返回

          mysql> select reverse("abcdef");
          +-------------------+
          | reverse("abcdef") |
          +-------------------+
          | fedcba |
          +-------------------+
          1 row in set (0.06 sec)


          5.10 format()

          mysql> select format(129021,4);
          +------------------+
          | format(129021,4) |
          +------------------+
          | 129,021.0000 |
          +------------------+
          1 row in set (0.07 sec)


          5.11 lpad()

          位數(shù)不夠左邊補(bǔ)0

          mysql> select lpad("hello",10,"0");
          +----------------------+
          | lpad("hello",10,"0") |
          +----------------------+
          | 00000hello |
          +----------------------+
          1 row in set (0.13 sec)


          5.12 rpad()

          位數(shù)不夠右邊補(bǔ)0

          mysql> select rpad("hello",10,"0");
          +----------------------+
          | rpad("hello",10,"0") |
          +----------------------+
          | hello00000 |
          +----------------------+
          1 row in set (0.13 sec)


          5.13 instr()

          返回字串第一次出現(xiàn)的位置

          mysql> select instr("footer","ter"); 
          +-----------------------+
          | instr("footer","ter") |
          +-----------------------+
          | 4 |
          +-----------------------+
          1 row in set (0.07 sec)


          5.14 concat()

          mysql> select concat("My","S","QL");
          +--------------------+
          | concat("My","S","QL") |
          +--------------------+
          | MySQL |
          +--------------------+
          1 row in set (0.07 sec)


          鏈接:https://www.cnblogs.com/ciel717/p/17449389.html

                                                                        (版權(quán)歸原作者所有,侵刪)


          瀏覽 27
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人免费在线视频网站 | 天天操b视频 | 色影音先锋色资源网站 | 农村黄色录像一级天天干 | 蜜桃传媒一区二区亚洲 |