mysql字符串函數


https://dev.mysql.com/doc/refman/8.0/en/string-functions.html

如果字符串值函數的長度大於max_allowed_packet系統變量的值,則字符串值函數返回NULL。
對於操作字符串位置的函數,第一個位置的編號爲1。 對於接受長度參數的函數,非整數參數四捨五入到最近的整數。

ASCII(str)

Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string. Returns NULL if str is NULL. ASCII() works for 8-bit characters.
返回字符串str最左邊一個字符的ASCII值的十進制的表現形式。如果str是空字符串,則返回0。如果str爲空,則返回NULL。ASCII()適用於8位字符。ASCII參考 10進制
https://simple.wikipedia.org/wiki/ASCII

SELECT ASCII('2'),ASCII(2),ASCII('d'),ASCII('ds');

在這裏插入圖片描述

BIN(N)

Returns a string representation of the binary value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,2). Returns NULL if N is NULL.
返回數字N的二進制的字符串形式,N爲number

SELECT BIN(12),BIN(-12)  -- 1100  (8421碼)=8+4=12  返回一個數的二進制  正整數的原碼、反碼和補碼是一樣的

在這裏插入圖片描述

BIT_LENGTH(str)

Returns the length of the string str in bits.
返回這個字符串的位數

SELECT BIT_LENGTH('text'); -- 32 一個字符佔8位 1個字節
SELECT BIT_LENGTH('tex'); -- 24  
SELECT BIT_LENGTH(tex)   -- Unknown column 'tex' in 'field list'
SELECT BIT_LENGTH('中國'); -- 48 一個漢子佔24位  3個字節
	  
SELECT BIT_LENGTH('好');  -- 24
	  
SELECT BIT_LENGTH(1);  -- 8
SELECT BIT_LENGTH(11);  -- 16

CHAR(N,… [USING charset_name])16進制

CHAR() interprets each argument N as an integer and returns a string consisting of the characters given by the code values of those integers. NULL values are skipped.
CHAR()將每個參數N解釋爲一個整數,並返回ASCII十進制等於這些整數的的 ASCII字符 空值被跳過。

SELECT CHAR(77,121,83,81,'76'); -- MySQL
SELECT CHAR(77,77.3,'77.3'); -- MMM

CHAR() arguments larger than 255 are converted into multiple result bytes. For example, CHAR(256) is equivalent to CHAR(1,0), and CHAR(256256) is equivalent toCHAR(1,0,0)
大於255的CHAR()參數被轉換成多個結果字節。例如,CHAR(256)等價於CHAR(1,0), CHAR(256
256)等價於CHAR(1,0,0)
在這裏插入圖片描述
By default, CHAR() returns a binary string. To produce a string in a given character set, use the optional USING clause:
默認情況下,CHAR()返回一個二進制字符串。若要在給定字符集中生成字符串,請使用可選的USING子句:
在這裏插入圖片描述

如果使用的是給定的,並且結果字符串對於給定的字符集是非法的,則會發出警告。此外,如果啓用了嚴格的SQL模式,CHAR()的結果將變爲NULL。
SELECT CHAR(77); – M
SELECT CHARSET(CHAR(77)) – binary

SELECT CHAR(65)    -- A
SELECT CHAR('65')  -- A
SELECT CHAR(X'65'); -- e  x(不區分大小寫)開頭的爲16進制
SELECT CHAR(x65);  -- 錯誤代碼: 1054 Unknown column 'x65' in 'field list'

CHAR_LENGTH(str)\CHARACTER_LENGTH(str)

Returns the length of the string str, measured in characters. A multibyte character counts as a single character. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
返回字符串str的長度,以字符爲度量單位。
多字節字符被視爲單個字符。
這意味着對於包含5個2字節字符的字符串,LENGTH()返回10,而CHAR_LENGTH()返回5。

SELECT CHAR_LENGTH('你好'),CHAR_LENGTH('hello')

在這裏插入圖片描述
CHARACTER_LENGTH() is a synonym for CHAR_LENGTH().
CHARACTER_LENGTH()是CHAR_LENGTH()的同義詞。

CONCAT(str1,str2,…)

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent nonbinary string form.
返回連接參數所產生的字符串。
可能有一個或多個參數。
如果所有參數都是非二進制字符串,則結果是非二進制字符串。
如果參數包含任何二進制字符串,則結果是一個二進制字符串。
將數值參數轉換爲其等效的非二進制字符串形式。
CONCAT() returns NULL if any argument is NULL.
如果任何參數爲null,CONCAT()返回NULL。
在這裏插入圖片描述
For quoted strings, concatenation can be performed by placing the strings next to each other:
對於帶引號的字符串,可以通過將字符串放在一起來執行連接:

在這裏插入圖片描述

SELECT '你好' '世界','你好''世界'; -- 字符串之間用空格分開

在這裏插入圖片描述

CONCAT_WS(separator,str1,str2,…)

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

CONCAT_WS()代表Concatenate With Separator,是CONCAT()的一種特殊形式。第一個參數是其餘參數的分隔符。分隔符被添加到要連接的字符串之間。分隔符可以是字符串,其他參數也可以是字符串。如果分隔符爲空,則結果爲空。在這裏插入圖片描述

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

ELT(N,str1,str2,str3,…)

ELT() returns the Nth element of the list of strings: str1 if N = 1, str2 if N = 2, and so on. Returns NULL if N is less than 1 or greater than the number of arguments.ELT() is the complement of FIELD().
ELT()返回字符串列表的第N個元素:str1(如果N = 1), str2(如果N = 2),以此類推。如果N小於1或大於參數的數量,則返回NULL。ELT()是FIELD()的補碼。

在這裏插入圖片描述

FIELD(str,str1,str2,str3,…)

返回str1、str2、str3、…列表。如果沒有找到str,則返回0。

如果FIELD()的所有參數都是字符串,那麼所有參數都將作爲字符串進行比較。如果所有參數都是數字,則將它們作爲數字進行比較。否則,將參數比較爲double。

如果str爲NULL,返回值爲0,因爲NULL不能與任何值進行相等比較。FIELD()是ELT()的補碼。

在這裏插入圖片描述

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,) character.
如果字符串str位於由N個子字符串組成的字符串列表中,則返回一個範圍爲1到N的值。
字符串列表是由字符分隔的子字符串組成的字符串。
如果第一個參數是常量字符串,第二個參數是SET類型的列,則FIND_IN_SET()函數被優化爲使用位算法。如果str不在strlist中,或者strlist是空字符串,則返回0。如果其中一個參數爲空,則返回NULL。如果第一個參數包含逗號(,)字符,則此函數不能正常工作。
在這裏插入圖片描述

FORMAT(X,D[,locale])

Formats the number X to a format like ‘#,###,###.##’, rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.

將數字X格式化爲’#,###,###。,四捨五入到D位小數,並以字符串形式返回結果。如果D爲0,則結果沒有小數點或小數部分。
The optional third parameter enables a locale to be specified to be used for the result number’s decimal point, thousands separator, and grouping between separators. Permissible locale values are the same as the legal values for the lc_time_names system variable (see Section 10.15, “MySQL Server Locale Support”). If no locale is specified, the default is ‘en_US’.
可選的第三個參數允許指定區域設置,用於結果號的小數點、千位分隔符和分隔符之間的分組。允許的區域設置值與lc_time_names系統變量的合法值相同(參見10.15節,“MySQL服務器區域設置支持”)。如果沒有指定區域設置,默認值是’en_US’。

小數的處理遵循四捨五入的規則

mysql> SELECT FORMAT(12332.123456, 4);
        -> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
        -> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
        -> '12,332'
mysql> SELECT FORMAT(12332.2,2,'de_DE');
        -> '12.332,20'

FROM_BASE64(str)

Takes a string encoded with the base-64 encoded rules used by TO_BASE64() and returns the decoded result as a binary string. The result is NULL if the argument isNULL or not a valid base-64 string. See the description of TO_BASE64() for details about the encoding and decoding rules.
接受一個用TO_BASE64()使用的base-64編碼規則編碼的字符串,並將解碼後的結果作爲二進制字符串返回。如果參數爲NULL或不是有效的base-64字符串,則結果爲NULL。有關編碼和解碼規則的詳細信息,請參閱TO_BASE64()的描述。

mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
        -> 'JWJj', 'abc'

HEX(str), HEX(N)

For a string argument str, HEX() returns a hexadecimal string representation of str where each byte of each character in str is converted to two hexadecimal digits. (Multibyte characters therefore become more than two digits.) The inverse of this operation is performed by the UNHEX() function.
對於字符串參數str, HEX()返回str的十六進制字符串表示形式,其中str中的每個字符的每個字節都轉換爲兩個十六進制數字。(因此,多字節字符會變成兩個以上的數字。)此操作的逆函數由UNHEX()函數執行。
For a numeric argument N, HEX() returns a hexadecimal string representation of the value of N treated as a longlong (BIGINT) number. This is equivalent toCONV(N,10,16). The inverse of this operation is performed by CONV(HEX(N),16,10).
對於數值參數N, HEX()返回一個十六進制字符串,表示作爲longlong (BIGINT)數字處理的N的值。它等價於CONV(N,10,16)此操作的逆操作由CONV(十六進制(N),16,10)執行。

mysql> SELECT X'616263', HEX('abc'), UNHEX(HEX('abc'));
        -> 'abc', 616263, 'abc'
mysql> SELECT HEX(255), CONV(HEX(255),16,10);
        -> 'FF', 255

SELECT HEX(16) -- 10
SELECT HEX(17) -- 11

SELECT HEX('a') -- 61
SELECT HEX(97)  -- 61

SELECT HEX('ab') -- 6161

SELECT UNHEX(HEX('ab')) 

SELECT UNHEX(X'61') 

SELECT X'616263', HEX('abc'), UNHEX(HEX('abc'));
 
SELECT HEX(255), CONV(HEX(255),16,10);

SELECT 0XA

MySql_十六進制值https://www.cnblogs.com/yufei121/p/6103336.html
十六進制值
MySQL支持十六進制值。在數字上下文中,十六進制數如同整數(64位精度)。在字符串上下文,如同二進制字符串,每對十六進制數字被轉換爲一個字符:

mysql> SELECT x'4D7953514C';
        -> 'MySQL'
mysql> SELECT 0xa+0;
        -> 10
mysql> SELECT 0x5061756c;
        -> 'Paul'

十六進制值的默認類型是字符串。如果想要確保該值作爲數字處理,可以使用CAST(…AS UNSIGNED):

mysql> SELECT 0x41,CAST(0x41 AS UNSIGNED);
        -> 'A',65

0x語法基於ODBC。十六進制字符串通常用於ODBC以便爲BLOB列提供值。
x’hexstring’語法基於標準SQL。
可以用HEX()函數將一個字符串或數字轉換爲十六進制格式的字符串:

mysql> SELECT HEX('cat');
        -> '636174'
mysql> SELECT 0x636174;
        -> 'cat'

INSERT(str,pos,len,newstr)

Returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr. Returns the original string if pos is not within the length of the string. Replaces the rest of the string from position pos if len is not within the length of the rest of the string. Returns NULL if any argument is NULL.

mysql> SELECT INSERT('Quadratic', 3, 4, 'What');
        -> 'QuWhattic'
mysql> SELECT INSERT('Quadratic', -1, 4, 'What');
        -> 'Quadratic'
mysql> SELECT INSERT('Quadratic', 3, 100, 'What');
        -> 'QuWhat'

返回字符串str,子字符串以pos位置開始,len字符由字符串newstr替換。如果pos不在字符串長度內,則返回原始字符串。如果len不在字符串其餘部分的長度範圍內,則從位置pos替換字符串的其餘部分。如果任何參數爲空,則返回NULL。

INSTR(str,substr)

Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.

返回字符串str中子字符串substr第一次出現的位置。這與LOCATE()的雙參數形式相同,只是參數的順序顛倒了。 從1開始

mysql> SELECT INSTR('foobarbar', 'bar');
        -> 4
mysql> SELECT INSTR('xbar', 'foobar');
        -> 0

LEFT(str,len)

Returns the leftmost len characters from the string str, or NULL if any argument is NULL.
返回字符串str中最左邊的len字符,如果參數爲空,則返回NULL。 從1開始

mysql> SELECT LEFT('foobarbar', 5);
        -> 'fooba'

LENGTH(str)

Returns the length of the string str, measured in bytes. A multibyte character counts as multiple bytes. This means that for a string containing five 2-byte characters,LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

mysql> SELECT LENGTH('text');
        -> 4

LOAD_FILE(file_name)

Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.
If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns NULL.
The character_set_filesystem system variable controls interpretation of file names that are given as literal strings.

mysql> UPDATE t
            SET blob_col=LOAD_FILE('/tmp/picture')
            WHERE id=1;

https://segmentfault.com/a/1190000009333563
https://jingyan.baidu.com/article/e8cdb32bf15b3437052bad17.html
https://dev.mysql.com/doc/refman/8.0/en/server-options.html#option_mysqld_secure-file-priv

讀取文件並以字符串的形式返回文件內容。要使用此函數,文件必須位於服務器主機上,必須指定文件的完整路徑名,並且必須具有文件特權。文件必須是所有人都可讀的,並且它的大小小於max_allowed_packet字節。如果secure_file_priv系統變量被設置爲非空目錄名,則要加載的文件必須位於該目錄中。

如果文件不存在或由於不滿足前面的條件之一而無法讀取,則函數返回NULL。

character_set_filesystem系統變量控制以文字字符串形式給出的文件名的解釋。

LOCATE(substr,str), LOCATE(substr,str,pos)

The first syntax returns the position of the first occurrence of substring substr in string str. The second syntax returns the position of the first occurrence of substring substr in string str, starting at position pos. Returns 0 if substr is not in str. Returns NULL if any argument is NULL.

mysql> SELECT LOCATE('bar', 'foobarbar');
        -> 4
mysql> SELECT LOCATE('xbar', 'foobar');
        -> 0
mysql> SELECT LOCATE('bar', 'foobarbar', 5);
        -> 7

第一個語法返回第一次出現的位置的子串的子串字符串str。
第二個語法返回第一次出現的位置字符串str子串的子串的開始位置pos。
返回0,如果字符串的子串不在str。返回NULL如果任何參數是NULL。

LOWER(str)

Returns the string str with all characters changed to lowercase according to the current character set mapping. The default is utf8mb4.

mysql> SELECT LOWER('QUADRATICALLY');
        -> 'quadratically'
LOWER() (and UPPER()) are ineffective when applied to binary strings (BINARY, VARBINARY, BLOB). To perform lettercase conversion, convert the string to a nonbinary string:
mysql> SET @str = BINARY 'New York';
mysql> SELECT LOWER(@str), LOWER(CONVERT(@str USING utf8mb4));
+-------------+------------------------------------+
| LOWER(@str) | LOWER(CONVERT(@str USING utf8mb4)) |
+-------------+------------------------------------+
| New York    | new york                           |
+-------------+------------------------------------+

For collations of Unicode character sets, LOWER() and UPPER() work according to the Unicode Collation Algorithm (UCA) version in the collation name, if there is one, and UCA 4.0.0 if no version is specified. For example, utf8mb4_0900_ai_ci and utf8_unicode_520_ci work according to UCA 9.0.0 and 5.2.0, respectively, whereasutf8_unicode_ci works according to UCA 4.0.0. See Section 10.10.1, “Unicode Character Sets”.
當將LOWER()(和UPPER())應用於二進制字符串(binary、VARBINARY、BLOB)時無效。要執行字母大小寫轉換,請將字符串轉換爲非二進制字符串:

LPAD(str,len,padstr)

Returns the string str, left-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.

mysql> SELECT LPAD('hi',4,'??');
        -> '??hi'
mysql> SELECT LPAD('hi',1,'??');
        -> 'h'

返回字符串str,用字符串padstr左填充爲len字符長度。如果str比len長,則返回值縮短爲len字符。

LTRIM(str)

Returns the string str with leading space characters removed.

mysql> SELECT LTRIM('  barbar');
        -> 'barbar'

MAKE_SET(bits,str1,str2,…)

Returns a set value (a string containing substrings separated by , characters) consisting of the strings that have the corresponding bit in bits set. str1 corresponds to bit 0, str2 to bit 1, and so on. NULL values in str1, str2, … are not appended to the result.

mysql> SELECT MAKE_SET(1,'a','b','c');
        -> 'a'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world');
        -> 'hello,world'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice',NULL,'world');
        -> 'hello'
mysql> SELECT MAKE_SET(0,'a','b','c');
        -> ''

https://blog.csdn.net/qq_41725312/article/details/83039525

OCT(N)

Returns a string representation of the octal value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,8). Returns NULL if N is NULL.

mysql> SELECT OCT(12);
        -> '14'
返回一個字符串表示的八進制值N,其中N是一個long - long (BIGINT)數字。它等於CONV(N,10,8)如果N爲空,返回NULL。
SELECT OCT('8') – 10

QUOTE(str)

Quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (), single quote (’), ASCII NUL, and Control+Z preceded by a backslash. If the argument is NULL, the return value is the word “NULL”without enclosing single quotation marks.

mysql> SELECT QUOTE('Don\'t!');
        -> 'Don\'t!'
mysql> SELECT QUOTE(NULL);
        -> NULL

For comparison, see the quoting rules for literal strings and within the C API in Section 9.1.1, “String Literals”, and Section 28.7.7.56, “mysql_real_escape_string_quote()”.
引用一個字符串來生成一個結果,該結果可以用作SQL語句中正確轉義的數據值。返回的字符串用單引號括起來,每個反斜槓()、單引號(’)、ASCII NUL和Control+Z的實例前都有一個反斜槓。如果參數爲NULL,返回值爲單詞“NULL”,不包含單引號。

QUOTE()經常用在防止sql注入

SELECT org_name FROM auth_org WHERE org_name LIKE QUOTE('%省區')   
SELECT org_name FROM auth_org WHERE org_name LIKE '%省區'   

在這裏插入圖片描述

REPEAT(str,count)

Returns a string consisting of the string str repeated count times. If count is less than 1, returns an empty string. Returns NULL if str or count are NULL.

mysql> SELECT REPEAT('MySQL', 3);
        -> 'MySQLMySQLMySQL'

返回由字符串str重複計數次數組成的字符串。如果count小於1,返回一個空字符串。如果str或count爲空,則返回NULL。

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching forfrom_str.

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
        -> 'WwWwWw.mysql.com'

返回字符串str,將出現的所有from_str字符串替換爲字符串to_str。REPLACE()在搜索from_str時執行區分大小寫的匹配。

REVERSE(str)
Returns the string str with the order of the characters reversed.
mysql> SELECT REVERSE(‘abc’);
-> ‘cba’
RIGHT(str,len)
Returns the rightmost len characters from the string str, or NULL if any argument is NULL.
mysql> SELECT RIGHT(‘foobarbar’, 4);
-> ‘rbar’

RPAD(str,len,padstr)

Returns the string str, right-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.

mysql> SELECT RPAD('hi',5,'?');
        -> 'hi???'
mysql> SELECT RPAD('hi',1,'?');
        -> 'h'

RTRIM(str)

Returns the string str with trailing space characters removed.

mysql> SELECT RTRIM('barbar   ');
        -> 'barbar'

SPACE(N)

Returns a string consisting of N space characters.

mysql> SELECT SPACE(6);
        -> '    

返回一個由N個空格字符組成的字符串。

SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)

The forms without a len argument return a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used for pos in any of the forms of this function.
For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

mysql> SELECT SUBSTRING('Quadratically',5);
        -> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);
        -> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
        -> 'ratica'
mysql> SELECT SUBSTRING('Sakila', -3);
        -> 'ila'
mysql> SELECT SUBSTRING('Sakila', -5, 3);
        -> 'aki'
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
        -> 'ki'

This function is multibyte safe.
If len is less than 1, the result is the empty string.
沒有len參數的表單從字符串str的pos位置開始返回一個子字符串。
有len參數的表單從字符串str的pos位置開始返回一個子字符串len字符
使用from的表單是標準的SQL語法。
也可以對pos使用負值,在這種情況下,子字符串的開頭是字符串末尾的pos字符,而不是開頭。
在此函數的任何形式中,pos都可以使用負值。

對於所有形式的SUBSTRING(),要從中提取子字符串的字符串中第一個字符的位置被認爲是1。

SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
        -> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
        -> 'mysql.com'

在分隔符delim出現計數之前,從字符串str返回子字符串。
如果count爲正數,則返回最後分隔符左邊的所有內容(從左邊計數)。
如果count爲負數,則返回最後分隔符右邊的所有內容(從右邊計數)。SUBSTRING_INDEX()在搜索delim時執行區分大小寫的匹配。

TO_BASE64(str)

Converts the string argument to base-64 encoded form and returns the result as a character string with the connection character set and collation. If the argument is not a string, it is converted to a string before conversion takes place. The result is NULL if the argument is NULL. Base-64 encoded strings can be decoded using the FROM_BASE64() function.
將字符串參數轉換爲以base-64編碼的形式,並使用連接字符集和排序規則以字符串的形式返回結果。如果參數不是字符串,則在轉換之前將其轉換爲字符串。如果參數爲空,則結果爲空。可以使用FROM_BASE64()函數解碼Base-64編碼的字符串。

mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
        -> 'JWJj', 'abc'

Different base-64 encoding schemes exist. These are the encoding and decoding rules used by TO_BASE64() and FROM_BASE64():
 The encoding for alphabet value 62 is ‘+’.
 The encoding for alphabet value 63 is ‘/’.
 Encoded output consists of groups of 4 printable characters. Each 3 bytes of the input data are encoded using 4 characters. If the last group is incomplete, it is padded with ‘=’ characters to a length of 4.
 A newline is added after each 76 characters of encoded output to divide long output into multiple lines.
 Decoding recognizes and ignores newline, carriage return, tab, and space.

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)

Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified, spaces are removed.

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'

返回刪除所有remstr前綴或後綴的字符串str。
如果沒有一個說明符同時給出、引導符或尾隨符,則假定兩者都有。
remstr是可選的,如果沒有指定,則刪除空格。

UNHEX(str)

For a string argument str, UNHEX(str) interprets each pair of characters in the argument as a hexadecimal number and converts it to the byte represented by the number. The return value is a binary string.
對於字符串參數str, UNHEX(str)將參數中的每對字符解釋爲十六進制數字,並將其轉換爲該數字所表示的字節。返回值是一個二進制字符串。

mysql> SELECT UNHEX('4D7953514C');
        -> 'MySQL'
mysql> SELECT X'4D7953514C';
        -> 'MySQL'
mysql> SELECT UNHEX(HEX('string'));
        -> 'string'
mysql> SELECT HEX(UNHEX('1267'));
        -> '1267'

The characters in the argument string must be legal hexadecimal digits: ‘0’ … ‘9’, ‘A’ … ‘F’, ‘a’ … ‘f’. If the argument contains any nonhexadecimal digits, the result is NULL:

參數字符串中的字符必須是合法的十六進制數字:‘0’ …“9”、“‘. .“F”、“‘. .“f”。如果參數包含任何非十六進制數字,則結果爲NULL:

mysql> SELECT UNHEX('GG');
+-------------+
| UNHEX('GG') |
+-------------+
| NULL        |
+-------------+

A NULL result can occur if the argument to UNHEX() is a BINARY column, because values are padded with 0x00 bytes when stored but those bytes are not stripped on retrieval. For example, ‘41’ is stored into a CHAR(3) column as '41 ’ and retrieved as ‘41’ (with the trailing pad space stripped), so UNHEX() for the column value returns ‘A’. By contrast ‘41’ is stored into a BINARY(3) column as ‘41\0’ and retrieved as ‘41\0’ (with the trailing pad 0x00 byte not stripped). ‘\0’ is not a legal hexadecimal digit, so UNHEX() for the column value returns NULL.
For a numeric argument N, the inverse of HEX(N) is not performed by UNHEX(). Use CONV(HEX(N),16,10) instead. See the description of HEX().

UPPER(str)

Returns the string str with all characters changed to uppercase according to the current character set mapping. The default is utf8mb4.

返回字符串str,根據當前字符集映射將所有字符更改爲大寫。默認爲utf8mb4。

mysql> SELECT UPPER('Hej');
        -> 'HEJ'

See the description of LOWER() for information that also applies to UPPER(). This included information about how to perform lettercase conversion of binary strings (BINARY, VARBINARY, BLOB) for which these functions are ineffective, and information about case folding for Unicode character sets.
This function is multibyte safe.
UCASE() used within views is rewritten as UPPER().

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章