(資料庫二)MySQL字串函式
1.ascii(str):返回字串str的第一個字元的ascii值(str是空串時返回0)
用法:mysql> select ascii('2');
-> 50
mysql> select ascii(2);
-> 50
mysql> select ascii('dete');
-> 100
2.ord(str):如果字串str句首是單位元組返回與ascii()函式返回的相同值。如果字串str句首是單位元組返回與ascii()函式返回的相同值。如果是一個多位元組字元,以格式返回((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...]
用法:mysql> select ord('2');
-> 50
3.conv(n,from_base,to_base):對數字n進位制轉換,並轉換為字串返回(任何引數為null時返回null,進位制範圍為2-36進位制,當to_base是負數時n作為有符號數否則作無符號數,conv以64位點精度工作)
用法:mysql> select conv("a",16,2);
-> '1010'
mysql> select conv("6e",18,8);
-> '172'
mysql> select conv(-17,10,-18);
-> '-h'
mysql> select conv(10+"10"+'10'+0xa,10,10);
-> '40'
4.bin(n):把n轉為二進位制值並以字串返回(n是bigint數字,等價於conv(n,10,2))
用法:mysql> select bin(12);
-> '1100'
5.oct(n):把n轉為八進位制值並以字串返回(n是bigint數字,等價於conv(n,10,8))
用法:mysql> select oct(12);
-> '14'
6.hex(n):把n轉為十六進位制並以字串返回(n是bigint數字,等價於conv(n,10,16))
用法:mysql> select hex(255);
-> 'ff'
7.char(n,...):返回由引數n,...對應的ascii程式碼字元組成的一個字串(引數是n,...是數字序列,null值被跳過)
用法:mysql> select char(77,121,83,81,'76');
-> 'mysql'
mysql> select char(77,77.3,'77.3');
-> 'mmm'
8.concat(str1,str2,...):把引數連成一個長字串並返回(任何引數是null時返回null)
用法:mysql> select concat('my', 's', 'ql');
-> 'mysql'
mysql> select concat('my', null, 'ql');
-> null
mysql> select concat(14.3);
-> '14.3'
9.length(str) 、octet_length(str) 、char_length(str) 、character_length(str) :返回字串str的長度(對於多位元組字元char_length僅計算一次)
用法:mysql> select length('text');
-> 4
mysql> select octet_length('text');
-> 4
10.locate(substr,str)、position(substr in str):返回字串substr在字串str第一次出現的位置(str不包含substr時返回0)
用法:mysql> select locate('bar', 'foobarbar');
-> 4
mysql> select locate('xbar', 'foobar');
-> 0
11.locate(substr,str,pos):返回字串substr在字串str的第pos個位置起第一次出現的位置(str不包含substr時返回0)
用法:mysql> select locate('bar', 'foobarbar',5);
-> 7
12.instr(str,substr):返回字串substr在字串str第一次出現的位置(str不包含substr時返回0)
用法:mysql> select instr('foobarbar', 'bar');
-> 4
mysql> select instr('xbar', 'foobar');
-> 0
13.lpad(str,len,padstr):用字串padstr填補str左端直到字串長度為len並返回
用法:mysql> select lpad('hi',4,'??');
-> '??hi'
14.rpad(str,len,padstr):用字串padstr填補str右端直到字串長度為len並返回
用法:mysql> select rpad('hi',5,'?');
-> 'hi???'
15.left(str,len):返回字串str的左端len個字元
用法:mysql> select left('foobarbar', 5);
-> 'fooba'
16.right(str,len):返回字串str的右端len個字元
用法:mysql> select right('foobarbar', 4);
-> 'rbar'
17.substring(str,pos,len)、substring(str from pos for len)、mid(str,pos,len):返回字串str的位置pos起len個字元
用法:mysql> select substring('quadratically',5,6);
-> 'ratica'
18.substring(str,pos)、substring(str from pos):返回字串str的位置pos起的一個子串
用法:mysql> select substring('quadratically',5);
-> 'ratically'
mysql> select substring('foobarbar' from 4);
-> 'barbar'
19.substring_index(str,delim,count):返回從字串str的第count個出現的分隔符delim之後的子串(count為正數時返回左端,否則返回右端子串)
用法:mysql> select substring_index('www.mysql.com', '.', 2);
-> 'www.mysql'
mysql> select substring_index('www.mysql.com', '.', -2);
-> 'mysql.com'
20.ltrim(str):返回刪除了左空格的字串str
用法:mysql> select ltrim(' barbar');
-> 'barbar'
21.rtrim(str):返回刪除了右空格的字串str
用法:mysql> select rtrim('barbar ');
-> 'barbar'
22.trim([[both | leading | trailing] [remstr] from] str):返回字首或字尾remstr被刪除了的字串str(位置引數預設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'
23.soundex(str):返回str的一個同音字符串(聽起來“大致相同”字串有相同的同音字符串,非數字字母字元被忽略,在a-z外的字母被當作母音)
用法:mysql> select soundex('hello');
-> 'h400'
mysql> select soundex('quadratically');
-> 'q36324'
24.space(n):返回由n個空格字元組成的一個字串
用法:mysql> select space(6);
-> ' '
25.replace(str,from_str,to_str):用字串to_str替換字串str中的子串from_str並返回
用法:mysql> select replace('www.mysql.com', 'w', 'ww');
-> 'wwwwww.mysql.com'
26.repeat(str,count):返回由count個字串str連成的一個字串(任何引數為null時返回null,count<=0時返回一個空字串)
用法:mysql> select repeat('mysql', 3);
-> 'mysqlmysqlmysql
27.reverse(str):顛倒字串str的字元順序並返回
用法:mysql> select reverse('abc');
-> 'cba
28.insert(str,pos,len,newstr):把字串str由位置pos起len個字元長的子串替換為字串newstr並返回
用法:mysql> select insert('quadratic', 3, 4, 'what');
-> 'quwhattic'
29.elt(n,str1,str2,str3,...):返回第n個字串(n小於1或大於引數個數返回null)
用法:mysql> select elt(1, 'ej', 'heja', 'hej', 'foo');
-> 'ej'
mysql> select elt(4, 'ej', 'heja', 'hej', 'foo');
-> 'foo'
30.field(str,str1,str2,str3,...):返回str等於其後的第n個字串的序號(如果str沒找到返回0)
用法:mysql> select field('ej', 'hej', 'ej', 'heja', 'hej','foo');
-> 2
mysql> select field('fo', 'hej', 'ej', 'heja', 'hej','foo');
-> 0
31.find_in_set(str,strlist) :返回str在字串集strlist中的序號(任何引數是null則返回null,如果str沒找到返回0,引數1包含","時工作異常)
用法:mysql> select find_in_set('b','a,b,c,d');
-> 2
32.make_set(bits,str1,str2,...):把引數1的數字轉為二進位制,假如某個位置的二進位制位等於1,對應位置的字串選入字串集並返回(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');
-> ''
33.export_set(bits,on,off,[separator,[number_of_bits]]):按bits排列字串集,只有當位等於1時插入字串on,否則插入off(separator預設值",",number_of_bits引數使用時長度不足補0而過長截斷)
用法:mysql> select export_set(5,'y','n',',',4)
-> y,n,y,n
34.lcase(str)、lower(str):返回小寫的字串str
用法:mysql> select lcase('quadratically');
-> 'quadratically'
35.ucase(str)、upper(str):返回大寫的字串str
用法:mysql> select ucase('quadratically');
-> 'quadratically'
36.load_file(file_name):讀入檔案並且作為一個字串返回檔案內容(檔案無法找到,路徑不完整,沒有許可權,長度大於max_allowed_packet會返回null)
用法:mysql> update table_name set blob_column=load_file("/tmp/picture") where id=1;