Home Database Mysql Tutorial Detailed explanation of MySQL string functions (recommended)

Detailed explanation of MySQL string functions (recommended)

Dec 15, 2016 pm 04:42 PM

1. ASCII

ASCII(str)

Returns the ASCII code value of the leftmost character of the string str. If str is the empty string, 0 is returned. If str is NULL, return NULL.

2. ORD

ORD(str)

If the leftmost character of the string str is a multi-byte character, use the format ((first byte ASCII code)*256+(second byte ASCII code))[*256+third byte ASCII code...] returns the ASCII code value of the character to return the multibyte character code. If the leftmost character is not a multibyte character. Returns the same value returned by the ASCII() function.

3. CONV

CONV(N,from_base,to_base)
Convert numbers between different number bases. Returns the string number of the number N, transformed from the base from_base to the base to_base, or NULL if any argument is NULL. The N parameter is interpreted as an integer, but can be specified as an integer or a string. The smallest basis is 2 and the largest basis is 36. If to_base is a negative number, N is treated as a signed number, otherwise, N is treated as an unsigned number. CONV works with 64-bit precision.

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'
Copy after login

4. BIN

BIN(n)

Returns a string representation of the binary value N, where N is a long integer (BIGINT) number, which is equivalent to CONV(N,10,2 ). If N is NULL, return NULL.

5. OCT

OCT(N)
Returns a string representation of the octal value N, where N is a long integer number, which is equivalent to CONV(N,10,8). If N is NULL, return NULL.

6. HEX

HEX(N)
Returns a string representation of the hexadecimal value N, where N is a long integer (BIGINT) number, which is equivalent to CONV(N,10,16). If N is NULL, return NULL.
mysql> select HEX(255);

7. CHAR

CHAR(N,...)
CHAR() interprets the parameters as integers and returns a string composed of the ASCII code characters of these integers. NULL values ​​are skipped.

mysql> select CHAR(77,121,83,81,'76');
  
-> 'MySQL'
  
mysql> select CHAR(77,77.3,'77.3');
  
-> 'MMM'
Copy after login

8. CONCAT/CONCAT_WS

•CONCAT(str1,str2,...)

Returns the string from the parameter connection. If any argument is NULL, NULL is returned. Can have more than 2 parameters. A numeric argument is converted to its equivalent string form.

mysql> select CONCAT('My', 'S', 'QL');
  
-> 'MySQL'
  
mysql> select CONCAT('My', NULL, 'QL');
  
-> NULL
  
mysql> select CONCAT(14.3);
  
-> '14.3'
Copy after login

•CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() represents CONCAT With Separator , is a special form of CONCAT(). The first parameter is the delimiter for the other parameters. The position of the delimiter is placed between the two strings to be concatenated. The delimiter can be a string or other parameters.

SELECT CONCAT_WS(";",id,title) FROM my_table LIMIT 100;
  
SELECT CONCAT_WS(";",'aa','bb') FROM my_table
Copy after login

Nine, LENGTH/OCTET_LENGTH/CHAR_LENGTH/CHARACTER_LENGTH

LENGTH(str)/OCTET_LENGTH(str): number of bytes

CHAR_LENGTH(str)/CHARACTER_LENGTH(str): number of characters

十、LOCATE

This function is multi-byte reliable.

LOCATE(substr,str)
Returns the position where the substring substr first appears in the string str. If substr is not in str, returns 0.

LOCATE(substr,str,pos)
Returns the position of the first occurrence of substring substr in string str, starting from position pos. If substr is not in str, return 0.

11. LPAD/RPAD

LPAD(str,len,padstr)
Returns the string str, fill it with the string padstr on the left until str is len characters long.
RPAD(str,len,padstr)
Returns the string str, padded on the right with the string padstr until str is len characters long.

Twelve, LELT/RIGHT

LEFT(str,len)
Returns the leftmost len ​​characters of the string str.

RIGHT(str,len)
Returns the rightmost len ​​characters of the string str.

13. SUBSTRING

SUBSTRING (str, pos, len)

Returns a substring of len characters from the string str, starting from position pos.

SUBSTRING(str,pos)

Returns a substring from the starting position pos of the string str.

Fourteen, SUBSTRING_INDEX

SUBSTRING_INDEX(str,delim,count)

Returns the substring after the count-th occurrence of the delimiter delim from the string str. If count is positive, return the last delimiter to the left (counting from the left) of all characters. If count is negative, returns all characters to the right of the last delimiter (counting from the right).
This function is reliable for multibytes.

mysql> select SUBSTRING_INDEX('www.mysql.com', '.', 2);
  
-> 'www.mysql'
  
mysql> select SUBSTRING_INDEX('www.mysql.com', '.', -2);
  
-> 'mysql.com'
Copy after login

Fifteen, TRIM/LTRIM/RTRIM

TRIM([BOTH | LEADING | TRAILING] [remstr] FROM] str)
Returns the string str with all remstr prefixes or suffixes removed. If no modifiers BOTH, LEADING or TRAILING are given, BOTH is assumed. If remstr is 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'
Copy after login

LTRIM(str)
Returns the string str with its leading space characters removed.

RTRIM(str)
Returns the string str with its trailing space characters removed.

Sixteen, SPACE

SPACE(N)
Returns a string composed of N space characters.

Seventeen, REPLACE

REPLACE(str,from_str,to_str)
Returns the string str, in which all occurrences of the string from_str are replaced by the string to_str.

mysql> select REPLACE('www.mysql.com', 'w', 'Ww');
  
-> 'WwWwWw.mysql.com'
Copy after login

18. REPEAT

REPEAT(str,count)
Returns a string consisting of the string str repeated countTimes times. if count <= 0, returns an empty string. If str or count is NULL, return NULL.

Nineteen, REVERSE

REVERSE(str)
返回颠倒字符顺序的字符串str。

二十、INSERT

INSERT(str,pos,len,newstr)
返回字符串str,在位置pos起始的子串且len个字符长得子串由字符串newstr代替。

mysql> select INSERT(&#39;Quadratic&#39;, 3, 4, &#39;What&#39;);
  
-> &#39;QuWhattic&#39;
Copy after login

二十一、ELT

ELT(N,str1,str2,str3,...)
如果N= 1,返回str1,如果N= 2,返回str2,等等。如果N小于1或大于参数个数,返回NULL。ELT()是FIELD()反运算。

二十二、FIELD

FIELD(str,str1,str2,str3,...)
返回str在str1, str2, str3, ...清单的索引。如果str没找到,返回0。FIELD()是ELT()反运算。

二十三、FIND_IN_SET

FIND_IN_SET(str,strlist)
如果字符串str在由N子串组成的表strlist之中,返回一个1到N的值。一个字符串表是被“,”分隔的子串组成的一个字符串。如果第一个参数是一个常数字符串并且第二个参数是一种类型为SET的列,FIND_IN_SET()函数被优化而使用位运算!如果str不是在strlist里面或如果strlist是空字符串,返回0。如果任何一个参数是NULL,返回NULL。如果第一个参数包含一个“,”,该函数将工作不正常。

二十四、MAKE_SET

MAKE_SET(bits,str1,str2,...)
返回一个集合 (包含由“,”字符分隔的子串组成的一个字符串),由相应的位在bits集合中的的字符串组成。str1对应于位0,str2对应位1,等等。在str1, str2, ...中的NULL串不添加到结果中。

mysql> SELECT MAKE_SET(1,&#39;a&#39;,&#39;b&#39;,&#39;c&#39;);
  
-> &#39;a&#39;
  
mysql> SELECT MAKE_SET(1 | 4,&#39;hello&#39;,&#39;nice&#39;,&#39;world&#39;);
  
-> &#39;hello,world&#39;
  
mysql> SELECT MAKE_SET(0,&#39;a&#39;,&#39;b&#39;,&#39;c&#39;);
  
-> &#39;&#39;
Copy after login

   

二十五、EXPORT_SET

EXPORT_SET(bits,on,off,[separator,[number_of_bits])

返回一个字符串,在这里对于在“bits”中设定每一位,你得到一个“on”字符串,并且对于每个复位(reset)的位,你得到一个“off”字符串。每个字符串用“separator”分隔(缺省“,”),并且只有“bits”的“number_of_bits” (缺省64)位被使用。

二十六、LOWER/LCASE/UPPER/UCASE

LCASE(str)/LOWER(str) :返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成小写。该函数对多字节是可靠的。

UCASE(str)/UPPER(str) :返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成大写。该函数对多字节是可靠的。

二十七、LOAD_FILE

LOAD_FILE(file_name)
读入文件并且作为一个字符串返回文件内容。文件必须在服务器上,你必须指定到文件的完整路径名,而且你必须有file权限。文件必须所有内容都是可读的并且小于max_allowed_packet。如果文件不存在或由于上面原因之一不能被读出,函数返回NULL。

以上就是小编为大家带来的MySQL字符串函数详解(推荐)全部内容了,更多相关文章请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles