mysql字串擷取函式
阿新 • • 發佈:2019-01-30
- MySQL 字串擷取函式:left(), right(), substring(), substring_index()。還有 mid(), substr()。其中,mid(), substr() 等價於 substring() 函式,substring() 的功能非常強大和靈活。
- 1. 字串擷取:left(str, length)
- mysql> select left('sqlstudy.com', 3);
- +-------------------------+
- | left('sqlstudy.com', 3) |
- +-------------------------+
- | sql |
- +-------------------------+
- 2. 字串擷取:right(str, length)
- mysql> select right('sqlstudy.com', 3);
- +--------------------------+
- | right('sqlstudy.com', 3) |
- +--------------------------+
- | com |
- +--------------------------+
- 3. 字串擷取:substring(str, pos); substring(str, pos, len)
- 3.1 從字串的第 4 個字元位置開始取,直到結束。
- mysql> select substring('sqlstudy.com', 4);
- +------------------------------+
- | substring('sqlstudy.com', 4) |
- +------------------------------+
- | study.com |
- +------------------------------+
- 3.2 從字串的第 4 個字元位置開始取,只取 2 個字元。
- mysql> select substring('sqlstudy.com', 4, 2);
- +---------------------------------+
- | substring('sqlstudy.com', 4, 2) |
- +---------------------------------+
- | st |
- +---------------------------------+
- 3.3 從字串的第 4 個字元位置(倒數)開始取,直到結束。
- mysql> select substring('sqlstudy.com', -4);
- +-------------------------------+
- | substring('sqlstudy.com', -4) |
- +-------------------------------+
- | .com |
- +-------------------------------+
- 3.4 從字串的第 4 個字元位置(倒數)開始取,只取 2 個字元。
- mysql> select substring('sqlstudy.com', -4, 2);
- +----------------------------------+
- | substring('sqlstudy.com', -4, 2) |
- +----------------------------------+
- | .c |
- +----------------------------------+
- 我們注意到在函式 substring(str,pos, len)中, pos 可以是負值,但 len 不能取負值。
- 4. 字串擷取:substring_index(str,delim,count)
- 4.1 擷取第二個 '.' 之前的所有字元。
- mysql> select substring_index('www.sqlstudy.com.cn', '.', 2);
- +------------------------------------------------+
- | substring_index('www.sqlstudy.com.cn', '.', 2) |
- +------------------------------------------------+
- | www.sqlstudy |
- +------------------------------------------------+
- 4.2 擷取第二個 '.' (倒數)之後的所有字元。
- mysql> select substring_index('www.sqlstudy.com.cn', '.', -2);
- +-------------------------------------------------+
- | substring_index('www.sqlstudy.com.cn', '.', -2) |
- +-------------------------------------------------+
- | com.cn |
- +-------------------------------------------------+
- 4.3 如果在字串中找不到 delim 引數指定的值,就返回整個字串
- mysql> select substring_index('www.sqlstudy.com.cn', '.coc', 1);
- +---------------------------------------------------+
- | substring_index('www.sqlstudy.com.cn', '.coc', 1) |
- +---------------------------------------------------+
- | www.sqlstudy.com.cn |
- +---------------------------------------------------+
- 4.4 擷取一個表某個欄位資料的中間值 如該欄位資料為 1,2,3
- mysql> select substring_index(substring_index(該欄位, ',', 2) , ',', -1) from 表名;
- +--------------------------------------------------------------+
- | substring_index(substring_index(該欄位, ',', 2); , ',', -1)|
- +--------------------------------------------------------------+
- | 2 |
- +--------------------------------------------------------------+
- ASCII(str)
- 返回字串str的最左面字元的ASCII程式碼值。如果str是空字串,返回0。如果str是NULL,返回NULL。
- Java程式碼
- 如:select ASCII('2');select ASCII(2); 結果都為2的Ascii碼值 50
- 如:select ASCII('2');select ASCII(2); 結果都為2的Ascii碼值 50
- CHAR(N,...)
- CHAR()將引數解釋為整數並且返回由這些整數的ASCII程式碼字元組成的一個字串。NULL值被跳過。
- Java程式碼
- select CHAR(77,121,83,81,NULL); 結果為 MySQ ;
- select CHAR(77,121,83,81,NULL); 結果為 MySQ ;
- [color=darkred]CONCAT(str1,str2,...)
- 返回來自於引數連結的字串。如果任何引數是NULL,返回NULL。可以有超過2個的引數。一個數字引數被變換為等價的字串形式。
- Java程式碼
- select CONCAT('My', 'S', 'QL'); //MySQL
- select CONCAT(12.3); //'12.3'
- select CONCAT('My', 'S', 'QL'); //MySQL
- select CONCAT(12.3); //'12.3'
- LENGTH(str)
- 計算字串長度 :select length('text') ; //4
- LOCATE(substr,str)
- POSITION(substr IN str)
- 返回子串substr在字串str第一個出現的位置,如果substr不是在str裡面,返回0.
- Java程式碼
- 1.mysql> select LOCATE('bar', 'foobarbar');
- 2. -> 4
- 3.mysql> select LOCATE('xbar', 'foobar');
- 4. -> 0
- mysql> select LOCATE('bar', 'foobarbar');
- -> 4
- mysql> select LOCATE('xbar', 'foobar');
- -> 0該函式是多位元組可靠的
- LOCATE(substr,str,pos)
- 返回子串substr在字串str第一個出現的位置,從位置pos開始。如果substr不是在str裡面,返回0。
- Java程式碼
- 1.mysql> select LOCATE('bar', 'foobarbar',5);
- 2. -> 7
- mysql> select LOCATE('bar', 'foobarbar',5);
- -> 7
- INSTR(str,substr)
- 返回子串substr在字串str中的第一個出現的位置。這與有2個引數形式的LOCATE()相同,除了引數被顛倒。
- Java程式碼
- 1.mysql> select INSTR('foobarbar', 'bar');
- 2. -> 4
- 3.mysql> select INSTR('xbar', 'foobar');
- 4. -> 0
- mysql> select INSTR('foobarbar', 'bar');
- -> 4
- mysql> select INSTR('xbar', 'foobar');
- -> 0這函式是多位元組可靠的。
- LPAD(str,len,padstr)
- 返回字串str,左面用字串padstr填補直到str是len個字元長。
- Java程式碼
- 1.<PRE class=java name="code">mysql> select LPAD('hi',4,'??');
- 2. -> '??hi'
- 3. [color=darkred]RPAD(str,len,padstr)[/color] </PRE>返回字串str,右面用字串padstr填補直到str是len個字元長。
- Java程式碼 mysql> select LPAD('hi',4,'??'); -> '??hi'
- mysql> select LPAD('hi',4,'??');
- -> '??hi'
- [color=darkred]RPAD(str,len,padstr)[/color]
- 返回字串str,右面用字串padstr填補直到str是len個字元長。
- mysql> select RPAD('hi',5,'?');
- -> 'hi???'
- LEFT(str,len)
- 返回字串str的最左面len個字元。
- Java程式碼
- 1.mysql> select LEFT('foobarbar', 5);
- 2. -> 'fooba'該函式是多位元組可靠的。
- mysql> select LEFT('foobarbar', 5);
- -> 'fooba'該函式是多位元組可靠的。
- RIGHT(str,len)
- 返回字串str的最右面len個字元。
- Java程式碼
- 1.mysql> select RIGHT('foobarbar', 4);
- 2. -> 'rbar'
- mysql> select RIGHT('foobarbar', 4);
- -> 'rbar'
- 該函式是多位元組可靠的。
- SUBSTRING(str,pos)
- SUBSTRING(str FROM pos)
- 從字串str的起始位置pos返回一個子串。
- mysql> select SUBSTRING('Quadratically',5);
- -> 'ratically'
- mysql> select SUBSTRING('foobarbar' FROM 4);
- -> 'barbar'
- 該函式是多位元組可靠的。
- SUBSTRING_INDEX(str,delim,count)
- 返回從字串str的第count個出現的分隔符delim之後的子串。如果count是正數,返回最後的分隔符到左邊(從左邊數) 的所有字元。如果count是負數,返回最後的分隔符到右邊的所有字元(從右邊數)。
- mysql> select SUBSTRING_INDEX('www.mysql.com', '.', 2);
- -> 'www.mysql'
- mysql> select SUBSTRING_INDEX('www.mysql.com', '.', -2);
- -> 'mysql.com'
- 該函式對多位元組是可靠的。
- LTRIM(str)
- 返回刪除了其前置空格字元的字串str。
- mysql> select LTRIM(' barbar');
- -> 'barbar'
- RTRIM(str)
- 返回刪除了其拖後空格字元的字串str。
- mysql> select RTRIM('barbar ');
- -> 'barbar'
- 該函式對多位元組是可靠的。
- TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
- 返回字串str,其所有remstr字首或字尾被刪除了。如果沒有修飾符BOTH、LEADING或TRAILING給出,BOTH被假定。如果remstr沒被指定,空格被刪除。
- mysql> select TRIM(' bar ');
- -> 'bar'
- mysql> select TRIM(LEADING 'x' FROM 'xxxbarxxx');
- -> 'barxxx'
- mysql> select TRIM(BOTH 'x' FROM 'xxxbarxxx');
- -> 'bar'
- mysql> select TRIM(TRAILING 'xyz' FROM 'barxxyz');
- -> 'barx'
- 該函式對多位元組是可靠的。
- SOUNDEX(str)
- 返回str的一個同音字符串。聽起來“大致相同”的2個字串應該有相同的同音字符串。一個“標準”的同音字符串長是4個字元,但是SOUNDEX()函式返回一個任意長的字串。你可以在結果上使用SUBSTRING()得到一個“標準”的 同音串。所有非數字字母字元在給定的字串中被忽略。所有在A-Z之外的字元國際字母被當作母音。
- mysql> select SOUNDEX('Hello');
- -> 'H400'
- mysql> select SOUNDEX('Quadratically');
- -> 'Q36324'
- SPACE(N)
- 返回由N個空格字元組成的一個字串。
- mysql> select SPACE(6);
- -> ' '
- REPLACE(str,from_str,to_str)
- 返回字串str,其字串from_str的所有出現由字串to_str代替。
- mysql> select REPLACE('www.mysql.com', 'w', 'Ww');
- -> 'WwWwWw.mysql.com'
- 該函式對多位元組是可靠的。
- REPEAT(str,count)
- 返回由重複countTimes次的字串str組成的一個字串。如果count <= 0,返回一個空字串。如果str或count是NULL,返回NULL。
- mysql> select REPEAT('MySQL', 3);
- -> 'MySQLMySQLMySQL'
- REVERSE(str)
- 返回顛倒字元順序的字串str。
- mysql> select REVERSE('abc');
- -> 'cba'
- 該函式對多位元組可靠的。
- INSERT(str,pos,len,newstr)
- 返回字串str,在位置pos起始的子串且len個字元長得子串由字串newstr代替。
- mysql> select INSERT('Quadratic', 3, 4, 'What');
- -> 'QuWhattic'
- 該函式對多位元組是可靠的。
- ELT(N,str1,str2,str3,...)
- 如果N= 1,返回str1,如果N= 2,返回str2,等等。如果N小於1或大於引數個數,返回NULL。ELT()是FIELD()反運算。
- mysql> select ELT(1, 'ej', 'Heja', 'hej', 'foo');
- -> 'ej'
- mysql> select ELT(4, 'ej', 'Heja', 'hej', 'foo');
- -> 'foo'
- FIELD(str,str1,str2,str3,...)
- 返回str在str1, str2, str3, ...清單的索引。如果str沒找到,返回0。FIELD()是ELT()反運算。
- mysql> select FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo');
- -> 2
- mysql> select FIELD('fo', 'Hej', 'ej', 'Heja', 'hej', 'foo');
- -> 0
- FIND_IN_SET(str,strlist)
- 如果字串str在由N子串組成的表strlist之中,返回一個1到N的值。一個字串表是被“,”分隔的子串組成的一個字串。如果第一個引數是一個常數字符串並且第二個引數是一種型別為SET的列,FIND_IN_SET()函式被優化而使用位運算!如果str不是在strlist裡面或如果strlist是空字串,返回0。如果任何一個引數是NULL,返回NULL。如果第一個引數包含一個“,”,該函式將工作不正常。
- mysql> SELECT FIND_IN_SET('b','a,b,c,d');
- -> 2
- MAKE_SET(bits,str1,str2,...)
- 返回一個集合 (包含由“,”字元分隔的子串組成的一個字串),由相應的位在bits集合中的的字串組成。str1對應於位0,str2對應位1,等等。在str1, str2, ...中的NULL串不新增到結果中。
- mysql> SELECT MAKE_SET(1,'a','b','c');
- -> 'a'
- mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world');
- -> 'hello,world'
- mysql> SELECT MAKE_SET(0,'a','b','c');
- -> ''
- EXPORT_SET(bits,on,off,[separator,[number_of_bits]])
- 返回一個字串,在這裡對於在“bits”中設定每一位,你得到一個“on”字串,並且對於每個復位(reset)的位,你得到一個“off”字串。每個字串用“separator”分隔(預設“,”),並且只有“bits”的“number_of_bits” (預設64)位被使用。
- mysql> select EXPORT_SET(5,'Y','N',',',4)
- -> Y,N,Y,N
- LCASE(str)
- LOWER(str) 返回字串str,根據當前字符集對映(預設是ISO-8859-1 Latin1)把所有的字元改變成小寫。該函式對多位元組是可靠的。
- mysql> select LCASE('QUADRATICALLY');
- -> 'quadratically'
- UCASE(str)
- 返回字串str,根據當前字符集對映(預設是ISO-8859-1 Latin1)把所有的字元改變成大寫。該函式對多位元組是可靠的。
- mysql> select UCASE('Hej');
- -> 'HEJ'
- 該函式對多位元組是可靠的。
- LOAD_FILE(file_name)
- 讀入檔案並且作為一個字串返回檔案內容。檔案必須在伺服器上,你必須指定到檔案的完整路徑名,而且你必須有file許可權。檔案必須所有內容都是可讀的並且小於max_allowed_packet。如果檔案不存在或由於上面原因之一不能被讀出,函式返回NULL。
- mysql> UPDATE table_name
- SET blob_column=LOAD_FILE("/tmp/picture")
- WHERE id=1;
- MySQL必要時自動變換數字為字串,並且反過來也如此:
- mysql> SELECT 1+"1";
- -> 2
- mysql> SELECT CONCAT(2,' test');
- -> '2 test'
- 如果你想要明確地變換一個數字到一個字串,把它作為引數傳遞到CONCAT()。
- 如果字串函式提供一個二進位制字串作為引數,結果字串也是一個二進位制字串。被變換到一個字串的數字被當作是一個二進位制字串。這僅影響比較。
- --------------------- 本文來自 餘安2017 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/mycms5/article/details/52725877?utm_source=copy