字元串函數 是最常用的的一種函數,在一個具體應用中通常會綜合幾個甚至幾類函數來實現相應的應用: 1、LOWER(column|str):將字元串參數值轉換為全小寫字母後返回 2、UPPER(column|str):將字元串參數值轉換為全大寫字母後返回 3、CONCAT(column|str1, co ...
字元串函數
是最常用的的一種函數,在一個具體應用中通常會綜合幾個甚至幾類函數來實現相應的應用:
1、LOWER(column|str):將字元串參數值轉換為全小寫字母後返回
mysql> select lower('SQL Course'); +---------------------+ | lower('SQL Course') | +---------------------+ | sql course | +---------------------+
2、UPPER(column|str):將字元串參數值轉換為全大寫字母後返回
mysql> select upper('Use MYsql'); +--------------------+ | upper('Use MYsql') | +--------------------+ | USE MYSQL | +--------------------+
3、CONCAT(column|str1, column|str2,...):將多個字元串參數首尾相連後返回
mysql> select concat('My','S','QL'); +-----------------------+ | concat('My','S','QL') | +-----------------------+ | MySQL | +-----------------------+
如果有任何參數為null,則函數返回null
mysql> select concat('My',null,'QL'); +------------------------+ | concat('My',null,'QL') | +------------------------+ | NULL | +------------------------+
如果參數是數字,則自動轉換為字元串
mysql> select concat(14.3,'mysql'); +----------------------+ | concat(14.3,'mysql') | +----------------------+ | 14.3mysql | +----------------------+
4、CONCAT_WS(separator,str1,str2,...):將多個字元串參數以給定的分隔符separator首尾相連後返回
mysql> select concat_ws(';','First name','Second name','Last name'); +-------------------------------------------------------+ | concat_ws(';','First name','Second name','Last name') | +-------------------------------------------------------+ | First name;Second name;Last name | +-------------------------------------------------------+
!!也就是函數圓括弧里的第一個項目用來指定分隔符
註意:如果有任何參數為null,則函數不返回null,而是直接忽略它
mysql> select concat_ws(',','id',null,'name'); +---------------------------------+ | concat_ws(',','id',null,'name') | +---------------------------------+ | id,name | +---------------------------------+
打開和關閉管道符號“|”的連接功能
PIPES_AS_CONCAT:將“||”視為字元串的連接操作符而非或運算符
|| 管道連接符:
基本格式:
mysql> select 列名1 || 列名2 || 列名3 from 表名;
在mysql中,進行上式連接查詢之後,會將查詢結果集在一列中顯示(字元串連接),列名是‘列名1 || 列名2 || 列名3’;
mysql> select s_no || s_name || s_age -> from student; +-------------------------+ | s_no || s_name || s_age | +-------------------------+ | 1001張三23 | | 1002李四19 | | 1003馬五20 | | 1004甲六17 | | 1005乙七22 | +-------------------------+
註意:
①如果不顯示結果,是因為sql_mode參數中沒有PIPES_AS_CONCAT,只要給sql_mode參數加入PIPES_AS_CONCAT,就可以實現像CONCAT一樣的功能;
②如果不給sql_mode參數加入PIPES_AS_CONCAT的話,|| 預設是or的意思,查詢結果是一列顯示是1。
5、SUBSTR(str,pos[,len]):從源字元串str中的指定位置pos開始取一個字串並返回
註意:
①len指定子串的長度,如果省略則一直取到字元串的末尾;len為負值表示從源字元串的尾部開始取起。
②函數SUBSTR()是函數SUBSTRING()的同義詞。
mysql> select substring('hello world',5); +----------------------------+ | substring('hello world',5) | +----------------------------+ | o world | +----------------------------+ mysql> select substr('hello world',5,3); +---------------------------+ | substr('hello world',5,3) | +---------------------------+ | o w | +---------------------------+ mysql> select substr('hello world',-5); +--------------------------+ | substr('hello world',-5) | +--------------------------+ | world | +--------------------------+
6、LENGTH(str):返回字元串的存儲長度
mysql> select length('text'),length('你好'); +----------------+------------------+ | length('text') | length('你好') | +----------------+------------------+ | 4 | 6 | +----------------+------------------+
註意:編碼方式不同字元串的存儲長度就不一樣(‘你好’:utf8是6,gbk是4)
7、CHAR_LENGTH(str):返回字元串中的字元個數
mysql> select char_length('text'),char_length('你好'); +---------------------+-----------------------+ | char_length('text') | char_length('你好') | +---------------------+-----------------------+ | 4 | 2 | +---------------------+-----------------------+
8、INSTR(str, substr):從源字元串str中返回子串substr第一次出現的位置
mysql> select instr('foobarbar','bar'); +--------------------------+ | instr('foobarbar','bar') | +--------------------------+ | 4 | +--------------------------+
9、LPAD(str, len, padstr):在源字元串的左邊填充給定的字元padstr到指定的長度len,返回填充後的字元串
mysql> select lpad('hi',5,'??'); +-------------------+ | lpad('hi',5,'??') | +-------------------+ | ???hi | +-------------------+
10、RPAD(str, len, padstr):在源字元串的右邊填充給定的字元padstr到指定的長度len,返回填充後的字元串
mysql> select rpad('hi',6,'??'); +-------------------+ | rpad('hi',6,'??') | +-------------------+ | hi???? | +-------------------+
11、TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str):
從源字元串str中去掉兩端、首碼或尾碼字元remstr並返回;
如果不指定remstr,則去掉str兩端的空格;
不指定BOTH、LEADING、TRAILING ,則預設為 BOTH。
mysql> select trim(' bar '); +-----------------+ | trim(' bar ') | +-----------------+ | bar | +-----------------+ mysql> select trim(leading 'x' from 'xxxbarxxx'); +------------------------------------+ | trim(leading 'x' from 'xxxbarxxx') | +------------------------------------+ | barxxx | +------------------------------------+ mysql> select trim(both 'x' from 'xxxbarxxx'); +---------------------------------+ | trim(both 'x' from 'xxxbarxxx') | +---------------------------------+ | bar | +---------------------------------+ mysql> select trim(trailing 'xyz' from 'barxxyz'); +-------------------------------------+ | trim(trailing 'xyz' from 'barxxyz') | +-------------------------------------+ | barx | +-------------------------------------+
12、REPLACE(str, from_str, to_str):在源字元串str中查找所有的子串form_str(大小寫敏感),找到後使用替代字元串to_str替換它。返回替換後的字元串
mysql> select replace('www.mysql.com','w','Ww'); +-----------------------------------+ | replace('www.mysql.com','w','Ww') | +-----------------------------------+ | WwWwWw.mysql.com | +-----------------------------------+
13、LTRIM(str),RTRIM(str):去掉字元串的左邊或右邊的空格(左對齊、右對齊)
mysql> SELECT ltrim(' barbar ') rs1, rtrim(' barbar ') rs2; +-----------+-----------+ | rs1 | rs2 | +-----------+-----------+ | barbar | barbar | +-----------+-----------+
14、REPEAT(str, count):將字元串str重覆count次後返回
mysql> select repeat('MySQL',3); +-------------------+ | repeat('MySQL',3) | +-------------------+ | MySQLMySQLMySQL | +-------------------+
15、REVERSE(str):將字元串str反轉後返回
mysql> select reverse('abcdef'); +-------------------+ | reverse('abcdef') | +-------------------+ | fedcba | +-------------------+
16、CHAR(N,... [USING charset_name]):將每個參數N解釋為整數(字元的編碼),並返回每個整數對應的字元所構成的字元串(NULL值被忽略)。
mysql> select char(77,121,83,81,'76'),char(77,77.3,'77.3'); +-------------------------+----------------------+ | char(77,121,83,81,'76') | char(77,77.3,'77.3') | +-------------------------+----------------------+ | MySQL | MMM | +-------------------------+----------------------+
預設情況下,函數返回二進位字元串,若想返回針對特定字元集的字元串,使用using選項
mysql> SELECT charset(char(0x65)), charset(char(0x65 USING utf8)); +---------------------+--------------------------------+ | charset(char(0x65)) | charset(char(0x65 USING utf8)) | +---------------------+--------------------------------+ | binary | utf8 | +---------------------+--------------------------------+
17、FORMAT(X,D[,locale]):以格式‘#,###,###.##’格式化數字X
D指定小數位數
locale指定國家語言(預設的locale為en_US)
mysql> SELECT format(12332.123456, 4),format(12332.2,0); +-------------------------+-------------------+ | format(12332.123456, 4) | format(12332.2,0) | +-------------------------+-------------------+ | 12,332.1235 | 12,332 | +-------------------------+-------------------+ mysql> SELECT format(12332.2,2,'de_DE'); +---------------------------+ | format(12332.2,2,'de_DE') | +---------------------------+ | 12.332,20 | +---------------------------+
18、SPACE(N):返回由N個空格構成的字元串
mysql> select space(3); +----------+ | space(3) | +----------+ | | +----------+
19、LEFT(str, len):返回最左邊的len長度的子串
mysql> select left('chinaitsoft',5); +-----------------------+ | left('chinaitsoft',5) | +-----------------------+ | china | +-----------------------+
20、RIGHT(str, len):返回最右邊的len長度的子串
mysql> select right('chinaitsoft',5); +------------------------+ | right('chinaitsoft',5) | +------------------------+ | tsoft | +------------------------+
21、STRCMP(expr1,expr2):如果兩個字元串是一樣的則返回0;如果第一個小於第二個則返回-1;否則返回1
mysql> select strcmp('text','text'); +-----------------------+ | strcmp('text','text') | +-----------------------+ | 0 | +-----------------------+ mysql> SELECT strcmp('text', 'text2'),strcmp('text2', 'text'); +-------------------------+-------------------------+ | strcmp('text', 'text2') | strcmp('text2', 'text') | +-------------------------+-------------------------+ | -1 | 1 | +-------------------------+-------------------------+