mysql字串函數有:1、LOWER,將字串參數值轉換為全小寫字母後回傳;2、UPPER,將字串參數值轉換為全大寫字母後回傳;3、CONCAT,將多個字串參數首尾連接後傳回;4、SUBSTR,從來源字串str中指定位置pos開始取。
mysql字串函數有:
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 | +-------------------------------------------------------+! !也就是函數圓括號裡的第一個項目用來指定分隔符號
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???? |+-------------------+
從來源字串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
| 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 | +-------------------------+-------------------------+
相关学习推荐:mysql视频教程
以上是mysql字串函數有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!