MySQL的一些內建函式(字串)

2021-09-24 17:38:08 字數 4228 閱讀 2697

函式名

描述示例

ascii(s)

返回字串 s 的第乙個字元的 ascii 碼。

返回 customername 字段第乙個字母的 ascii 碼:

select ascii(customername) as numcodeoffirstchar

from customers;

char_length(s)

返回字串 s 的字元數

返回字串 csdn 的字元數

select char_length("csdn") as lengthofstring;

concat(s1,s2...sn)

字串 s1,s2 等多個字串合併為乙個字串

合併多個字串

select concat("sql ", "csdn ", "google ", "facebook") as concatenatedstring;

concat_ws(x, s1,s2...sn)

同 concat(s1,s2,...) 函式,但是每個字串之間要加上 x,x 可以是分隔符

合併多個字串,並新增分隔符:

select concat_ws("-", "sql", "tutorial", "is", "fun!")as concatenatedstring;

field(s,s1,s2...)

返回第乙個字串 s 在字串列表(s1,s2...)中的位置

返回字串 c 在列表值中的位置:

select field("c", "a", "b", "c", "d", "e");

find_in_set(s1,s2)

返回在字串s2中與s1匹配的字串的位置

返回字串 c 在指定字串中的位置:

select find_in_set("c", "a,b,c,d,e");

format(x,n)

函式可以將數字 x 進行格式化 "#,###.##", 將 x 保留到小數點後 n 位,最後一位四捨五入。

格式化數字 "#,###.##" 形式:

select format(250500.5634, 2);     -- 輸出 250,500.56

insert(s1,x,len,s2)

字串 s2 替換 s1 的 x 位置開始長度為 len 的字串

從字串第乙個位置開始的 6 個字元替換為 runoob:

select insert("baidu.com", 1, 6, "csdn");  -- 輸出:csdn.com

locate(s1,s)

從字串 s 中獲取 s1 的開始位置

獲取 b 在字串 abc 中的位置:

select locate('st','myteststring');  -- 5
返回字串 abc 中 b 的位置:

select locate('b', 'abc') -- 2

lcase(s)

將字串 s 的所有字母變成小寫字母

字串 runoob 轉換為小寫:

select lcase('csdn') -- csdn

left(s,n)

返回字串 s 的前 n 個字元

返回字串 runoob 中的前兩個字元:

select left('csdn',2) -- cs

lower(s)

與lcase(s)相同

lpad(s1,len,s2)

在字串 s1 的開始處填充字串 s2,使字串長度達到 len

將字串 xx 填充到 abc 字串的開始處:

select lpad('abc',5,'xx') -- xxabc

mid(s,n,len)

從字串 s 的 start 位置擷取長度為 length 的子字串,與 substring(s,n,len)相同

從字串 runoob 中的第 2 個位置擷取 3個 字元:

select mid("csdn", 2, 3) as extractstring; -- sdn

position(s1 in s)

從字串 s 中獲取 s1 的開始位置

返回字串 abc 中 b 的位置:

select position('b' in 'abc') -- 2

repeat(s,n)

將字串 s 重複 n 次

將字串 runoob 重複三次:

select repeat('csdn',3) -- csdncsdncsdn

replace(s,s1,s2)

將字串 s2 替代字串 s 中的字串 s1

將字串 abc 中的字元 a 替換為字元 x:

select replace('abc','a','x') --xbc

reverse(s)

將字串s的順序反過來

將字串 abc 的順序反過來:

select reverse('abc') -- cba

right(s,n)

返回字串 s 的後 n 個字元

返回字串 runoob 的後兩個字元:

select right('csdn',2) -- dn

rpad(s1,len,s2)

在字串 s1 的結尾處新增字串 s2,使字串的長度達到 len

將字串 xx 填充到 abc 字串的結尾處:

select rpad('abc',5,'xx') -- abcxx

rtrim(s)

去掉字串 s 結尾處的空格

去掉字串 runoob 的末尾空格:

select rtrim("csdn     ") as righttrimmedstring;  

-- csdn

space(n)

返回 n 個空格

返回 10 個空格:

select space(10);

strcmp(s1,s2)

比較字串 s1 和 s2,如果 s1 與 s2 相等返回 0 ,如果 s1>s2 返回 1,如果 s1比較字串:

select strcmp("runoob", "runoob");  -- 0

substr(s, start, length)

從字串 s 的 start 位置擷取長度為 length 的子字串

從字串 runoob 中的第 2 個位置擷取 3個 字元:

select substr("csdncsdn", 2, 3) as extractstring; -- sdn

substring(s, start, length)

同substr(s, start, length)

substring_index(s, delimiter, number)

返回從字串 s 的第 number 個出現的分隔符 delimiter 之後的字串。

如果 number 是正數,返回第 number 個字元左邊的字串。

如果 number 是負數,返回第(number 的絕對值(從右邊數))個字元右邊的字串。

select substring_index('a*b','*',1) -- a

select substring_index('a*b','*',-1)    -- b

select substring_index(substring_index('a*b*c*d*e','*',3),'*',-1)    -- c

trim(s)

去掉字串 s 開始和結尾處的空格

去掉字串 runoob 的首尾空格:

select trim('    runoob    ') as trimmedstring;

ucase(s)

將字串轉換為大寫

將字串 runoob 轉換為大寫:

select ucase("runoob"); -- runoob

upper(s)

同ucase(s)

c 一些常見的內建函式(字串)

int a 1 double b 0.0 string digit1 to string a string digit2 to string b cout digit1 endl 1 cout digit2 endl 0.000000 int a 1 string c 2.9 string digi...

Python的一些字串內建函式

方法 描述string.capitalize 把字串的第乙個字元大寫 string.center width 返回乙個原字串居中,並使用空格填充至長度width 的新字串 string.count str,beg 0,end len string 返回str 在string 裡面出現的次數,如果be...

一些字串函式

1.right location,somenumber left location,somenumber select right location,2 from my contacts 返回location列中所有右數兩個字元 select left location,2 from my cont...