MySQL字符串函數


1,UPPER和UCASE

把字符串轉成大小寫.

mysql> select name,UPPER(name) from string_test where name='tank';
+------+-------------+
| name | UPPER(name) |
+------+-------------+
| tank | TANK        |
+------+-------------+
1 row in set (0.00 sec)
2,LOWER和LCASE
把字符串轉成小寫
mysql> select sex,LCASE(job) from string_test where job='DUCK';
+------+------------+
| sex  | LCASE(job) |
+------+------------+
|    1 | duck       |
+------+------------+
1 row in set (0.00 sec)
3,FIND_IN_SET(str,strlist)

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

返回str在str1, str2, str3, …清單的索引。如果str沒找到,返回0。FIELD()是ELT()反運算。

mysql> SELECT id, name, FIELD( id, name, sex, job, hobby )
 -> FROM string_test where id < 4;
+----+-------+------------------------------------+
| id | name  | FIELD( id, name, sex, job, hobby ) |
+----+-------+------------------------------------+
|  1 | tank  |                                  2 |
|  2 | zhang |                                  0 |
|  3 | ying  |                                  0 |
+----+-------+------------------------------------+
3 rows in set (0.00 sec)

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

如果N= 1,返回str1,如果N= 2,返回str2,等等。如果N小於1或大於參數個數,返回NULL。ELT()是FIELD()反運算。

mysql> SELECT id, name, ELT(1, id, name, sex, job, hobby ) FROM string_test where id < 4;
+----+-------+------------------------------------+
| id | name  | ELT(1, id, name, sex, job, hobby ) |
+----+-------+------------------------------------+
|  1 | tank  | 1                                  |
|  2 | zhang | 2                                  |
|  3 | ying  | 3                                  |
+----+-------+------------------------------------+
3 rows in set (0.00 sec)
mysql> SELECT id, name, ELT(2, id, name, sex, job, hobby ) FROM string_test where id < 4;
+----+-------+------------------------------------+
| id | name  | ELT(2, id, name, sex, job, hobby ) |
+----+-------+------------------------------------+
|  1 | tank  | tank                               |
|  2 | zhang | zhang                              |
|  3 | ying  | ying                               |
+----+-------+------------------------------------+
3 rows in set (0.00 sec)

6,REPLACE(str,from_str,to_str)

返回字符串str,其字符串from_str的所有出現由字符串to_str代替。

mysql> SELECT id,REPLACE(hobby,"firend",'living') FROM string_test WHERE id = 2;

+----+----------------------------------+
| id | REPLACE(hobby,"firend",'living') |
+----+----------------------------------+
|  2 | fly,make living                  |
+----+----------------------------------+
1 row in set (0.00 sec)

7,REPEAT(str,count)

返回由重復countTimes次的字符串str組成的一個字符串。如果count <= 0,返回一個空字符串。如果str或count是NULL,返回NULL。

mysql> SELECT id,REPEAT(name,2) FROM string_test WHERE id > 1 and id < 4;
+----+----------------+
| id | REPEAT(name,2) |
+----+----------------+
|  2 | zhangzhang     |
|  3 | yingying       |
+----+----------------+
2 rows in set (0.00 sec)

8,REVERSE(str)

返回顛倒字符順序的字符串str。

 mysql> SELECT id,reverse(name) FROM string_test WHERE id > 1 and id < 4;
+----+---------------+
| id | reverse(name) |
+----+---------------+
|  2 | gnahz         |
|  3 | gniy          |
+----+---------------+
2 rows in set (0.00 sec)

9,INSERT(str,pos,len,newstr)

返回字符串str,在位置pos起始的子串且len個字符長得子串由字符串newstr代替。

mysql> select id,name,INSERT(hobby,10,6,'living') from string_test where id = 2;

+----+-------+-----------------------------+
| id | name  | INSERT(hobby,10,6,'living') |
+----+-------+-----------------------------+
|  2 | zhang | fly,make living             |
+----+-------+-----------------------------+
1 row in set (0.00 sec)

10,SUBSTRING(str FROM pos)

從字符串str的起始位置pos返回一個子串。下面的sub2沒有值,因為mysql數據庫的下標是從1開始的。

mysql> SELECT id, substring( hobby, 1, 6) AS sub1, substring( hobby from 0 for 8
 ) AS sub2,substring( hobby,2) AS sub3, substring( hobby from 4 ) AS sub4 FROM s
tring_test WHERE id =4;
+----+--------+------+-----------+---------+
| id | sub1   | sub2 | sub3      | sub4    |
+----+--------+------+-----------+---------+
|  4 | i love |      |  love you | ove you |
+----+--------+------+-----------+---------+
1 row in set (0.00 sec)

11,SUBSTRING_INDEX(str,delim,count)

返回從字符串str的第count個出現的分隔符delim之后的子串。如果count是正數,返回最后的分隔符到左邊(從左邊數) 的所有字符。如果count是負數,返回最后的分隔符到右邊的所有字符(從右邊數)。

mysql> SELECT id,SUBSTRING_INDEX(hobby,',',2) as test1,SUBSTRING_INDEX(hobby,','
,-1) as test2 FROM string_test WHERE id = 3;
+----+----------------+---------+
| id | test1          | test2   |
+----+----------------+---------+
|  3 | flying,driving | testing |
+----+----------------+---------+
1 row in set (0.01 sec)

12,LTRIM(str)

返回刪除了其前置空格字符的字符串str。

mysql> SELECT id,LTRIM(job) FROM string_test WHERE id = 4;
+----+----------------+
| id | LTRIM(job)     |
+----+----------------+
|  4 | love your love |
+----+----------------+
1 row in set (0.00 sec)

13,RTRIM(str)

返回刪除了其拖后空格字符的字符串str。

mysql> SELECT id,RTRIM(job) FROM string_test WHERE id = 4;
+----+----------------+
| id | RTRIM(job)     |
+----+----------------+
|  4 | love your love |
+----+----------------+
1 row in set (0.00 sec)

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

返回字符串str,其所有remstr前綴或后綴被刪除了。如果沒有修飾符BOTH、LEADING或TRAILING給出,BOTH被假定。如果remstr沒被指定,空格被刪除。

mysql> select trim(' test  ');
+-----------------+
| trim(' test  ') |
+-----------------+
| test            |
+-----------------+
1 row in set (0.01 sec)
mysql> SELECT id,TRIM(LEADING "love" from job) as test1,TRIM(BOTH "love" from jo
b) as test2,TRIM(TRAILING "love" from job) as test3 FROM string_test WHERE id =
4
 -> ;
+----+------------+--------+------------+
| id | test1      | test2  | test3      |
+----+------------+--------+------------+
|  4 |  your love |  your  | love your  |
+----+------------+--------+------------+
1 row in set (0.00 sec)

15,MID(str,pos,len)

從字符串str返回一個len個字符的子串,從位置pos開始。使用FROM的變種形式是ANSI SQL92語法。

mysql>  SELECT id, mid( hobby, 1, 6 ) AS sub1, mid( hobby
 -> FROM 0
 -> FOR 8 ) AS sub2, mid( hobby, 2 ) AS sub3, mid( hobby
 -> FROM 4 ) AS sub4
 -> FROM string_test
 -> WHERE id =4 ;
+----+--------+------+-----------+---------+
| id | sub1   | sub2 | sub3      | sub4    |
+----+--------+------+-----------+---------+
|  4 | i love |      |  love you | ove you |
+----+--------+------+-----------+---------+
1 row in set (0.00 sec)

16,LPAD(str,len,padstr)

返回字符串str,左面用字符串padstr填補直到str是len個字符長。

mysql> SELECT id,LPAD(name,11,"zhang ") FROM string_test WHERE id = 3;
+----+------------------------+
| id | LPAD(name,11,"zhang ") |
+----+------------------------+
|  3 | zhang zying            |
+----+------------------------+
1 row in set (0.00 sec)

17,RPAD(str,len,padstr)

返回字符串str,右面用字符串padstr填補直到str是len個字符長。

mysql> SELECT id,RPAD(name,11," ying") FROM string_test WHERE id = 2;
+----+-----------------------+
| id | RPAD(name,11," ying") |
+----+-----------------------+
|  2 | zhang ying            |
+----+-----------------------+
1 row in set (0.00 sec)

18,LEFT(str,len)

返回字符串str的最左面len個字符。

mysql> SELECT id,left(job,4) FROM string_test WHERE id = 4;
+----+-------------+
| id | left(job,4) |
+----+-------------+
|  4 | love        |
+----+-------------+
1 row in set (0.00 sec)

19,RIGHT(str,len)

返回字符串str的最右面len個字符。

mysql> SELECT id,right(job,4) FROM string_test WHERE id = 4;
+----+--------------+
| id | right(job,4) |
+----+--------------+
|  4 | love         |
+----+--------------+
1 row in set (0.00 sec)

20,位置控制函數

POSITION(substr IN str)
返回子串substr在字符串str第一個出現的位置,如果substr不是在str里面,返回0.

LOCATE(substr,str,pos)
返回子串substr在字符串str第一個出現的位置,從位置pos開始。如果substr不是在str里面,返回0。

INSTR(str,substr)
返回子串substr在字符串str中的第一個出現的位置。這與有2個參數形式的LOCATE()相同,除了參數被顛倒。

mysql> SELECT id,INSTR(job,"you") as instr,LOCATE('love',job,3) as locate,POSITI
ON('love' in job) as position FROM string_test WHERE id = 4;
+----+-------+--------+----------+
| id | instr | locate | position |
+----+-------+--------+----------+
|  4 |     6 |     11 |        1 |
+----+-------+--------+----------+
1 row in set (0.00 sec)

21,得到字符串長度的函數

LENGTH(str),OCTET_LENGTH(str),CHAR_LENGTH(str),CHARACTER_LENGTH(str)

mysql> SELECT id,LENGTH(job) as one,OCTET_LENGTH(job) as two,CHAR_LENGTH(job) as
 three,CHARACTER_LENGTH(job) as four FROM string_test WHERE id = 4;
+----+-----+-----+-------+------+
| id | one | two | three | four |
+----+-----+-----+-------+------+
|  4 |  14 |  14 |    14 |   14 |
+----+-----+-----+-------+------+
1 row in set (0.00 sec)

22,合並多個字符串,或者表中的多個字段

CONCAT(str1,str2,…)

返回來自於參數連結的字符串。如果任何參數是NULL,返回NULL。可以有超過2個的參數。一個數字參數被變換為等價的字符串形式。

mysql> SELECT id,CONCAT(name,job,hobby) FROM string_test WHERE id = 4;
+----+-----------------------------------+
| id | CONCAT(name,job,hobby)            |
+----+-----------------------------------+
|  4 | tankzhanglove your lovei love you |
+----+-----------------------------------+
1 row in set (0.00 sec)

23,進制轉換

BIN(N)
返回二進制值N的一個字符串表示,在此N是一個長整數(BIGINT)數字,這等價於CONV(N,10,2)。如果N是NULL,返回NULL。

OCT(N)
返回八進制值N的一個字符串的表示,在此N是一個長整型數字,這等價於CONV(N,10,8)。如果N是NULL,返回NULL。

HEX(N)
返回十六進制值N一個字符串的表示,在此N是一個長整型(BIGINT)數字,這等價於CONV(N,10,16)。如果N是NULL,返回NULL。

ASCII(str)
返回字符串str的最左面字符的ASCII代碼值。如果str是空字符串,返回0。如果str是NULL,返回NULL。

mysql> select bin(20),oct(20),hex(20),ascii(20);
+---------+---------+---------+-----------+
| bin(20) | oct(20) | hex(20) | ascii(20) |
+---------+---------+---------+-----------+
| 10100   | 24      | 14      |        50 |
+---------+---------+---------+-----------+
1 row in set (0.02 sec)

上面我只例舉了一部分對字符串進行操作的函數,並且是我覺得我們平時會用的,有可能會用到的一些函數。

 

在MySQL中進行字符串的拼接要使用CONCAT()函數,CONCAT()函數支持一個或者多個參數,參數類型可以為字符串類型也可以是非字符串類 型。對於非字符串類型的參數MySQL將嘗試將其轉化為字符串類型,CONCAT()函數會將所有參數按照參數的順序拼接成一個字符串作為返回值。比如下 面的SQL語句用於將用戶的多個字段信息以一個字段的形式查詢出來:

SELECT CONCAT('Staff Number :', 'Happiness Index', FNumber, FSalary/(FAge-21)) from t_employee

+----------------------------------------------------------+
| concat                                                   |
+----------------------------------------------------------+
| Staff number: Happiness index of DEV001 is 2075.000000   |
| Staff number: Happiness index of DEV002 is 328.571429    |
| NULL                                                                                     |
| Staff number: Happiness index of HR001 is 2150.000000    |
| Staff number: Happiness index of HR002 is 825.000000     |
| Staff number: Happiness index of IT001 is 785.714286     |
| Staff number: Happiness index of IT002 is 466.666667     |
| Staff number: Happiness index of SALES001 is 1325.000000 |
| Staff number: Happiness index of SALES002 is 592.857143  |
+----------------------------------------------------------+

MYSQL中還提供了另外一個進行字符串拼接的函數CONCAT_WS,CONCAT_WS可以在待拼接的字符串之間加入指定的分割符。它的第一個參數值為采用的分隔符,而剩下的參數則為待拼接的字符串值,比如執行下面的SQL:

SELECT CONCAT_WS(', ', FNumber,FAge, FDepartment, FSalary) FROM T_Employee
+------------------------------------------------------+
| concat_ws(', ', FNumber, FAge, FDepartment, FSalary) |
+------------------------------------------------------+
| DEV001, 25, Development, 8300.00                     |
| DEV002, 28, Development, 2300.00                     |
| DEV003, Development, 3333.00                         |
| HR001, 23, HumanResource, 4300.00                    |
| HR002, 25, HumanResource, 3300.00                    |
| IT001, 28, InfoTech, 5500.00                         |
| IT002, 27, InfoTech, 2800.00                         |
| SALES001, 25, Sales, 5300.00                         |
| SALES002, 35, Sales, 8300.00                         |
+------------------------------------------------------+

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM