MySQL's string function

Source: Internet
Author: User
Tags numeric ord

Note: If the result is longer than the server parameter Max_allowed_packet, the string value function returns null
For operations on string positions, the first position is marked as 1.

ASCII (str)
Returns the ASCII code value of the leftmost character of the string str. If Str is an empty string, returns 0. If STR is NULL, returns NULL.
Mysql> Select ASCII (' 2 ');
-> 50
Mysql> Select ASCII (2);
-> 50
Mysql> Select ASCII (' DX ');
-> 100
You can also see the Ord () function.


ORD (str)
If the string str leftmost character is a multibyte character, by using the format (the *256+ (the 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 with the ASCII () function.
Mysql> Select ORD (' 2 ');
-> 50

CONV (N,from_base,to_base)
Transforms a number between different numeric bases. Returns a string number for the number n, from the From_base base transformation to the to_base base, or null if any argument is null. Parameter n is interpreted as an integer, but can be specified as an integer or a string. The minimum base is 2 and the largest base is 36. If To_base is a negative number, n is considered to be a signed digit, otherwise n is treated as unsigned. Conv work 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+ "ten" + ' +0xa,10,10 ');
-> ' 40 '

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, returns NULL.
Mysql> Select BIN (12);
-> ' 1100 '

OCT (N)
Returns the representation of a string of octal value N, where n is a long integer number, which is equivalent to Conv (n,10,8). If n is null, returns NULL.
Mysql> Select OCT (12);
-> ' 14 '

HEX (N)
Returns the hexadecimal value n the representation of a string, where n is a long integer (BIGINT) number, which is equivalent to Conv (n,10,16). If n is null, returns NULL.
Mysql> Select HEX (255);
-> ' FF '

CHAR (N,...)
CHAR () interprets the argument as an integer and returns a string consisting of the ASCII code characters of these integers. The null value is skipped.
Mysql> Select CHAR (77,121,83,81, ' 76 ');
-> ' MySQL '
Mysql> Select CHAR (77,77.3, ' 77.3 ');
-> ' MMM '

CONCAT (STR1,STR2,...)
Returns a string from the argument's link. Returns null if any argument is null. can have more than 2 parameters. A numeric parameter is transformed into an equivalent string form.
Mysql> Select CONCAT (' My ', ' S ', ' QL ');
-> ' MySQL '
Mysql> Select CONCAT (' My ', NULL, ' QL ');
-> NULL
Mysql> Select CONCAT (14.3);
-> ' 14.3 '

LENGTH (str)

Octet_length (str)

Char_length (str)

Character_length (str)
Returns the length of the string str.
Mysql> Select LENGTH (' text ');
-> 4
Mysql> Select Octet_length (' text ');
-> 4

Note that for multibyte characters, its char_length () is evaluated only once.

LOCATE (SUBSTR,STR)

POSITION (substr in str)
Returns the substring substr the first occurrence of the string str, if SUBSTR is not inside STR, returns 0.
Mysql> Select LOCATE (' Bar ', ' Foobarbar ');
-> 4
Mysql> Select LOCATE (' Xbar ', ' foobar ');
-> 0

The function is multiple-byte reliable.
LOCATE (Substr,str,pos)
Returns the substring substr the position of the first occurrence of the string str, starting at the location POS. If SUBSTR is not inside STR, return 0.
Mysql> Select LOCATE (' Bar ', ' Foobarbar ', 5);
-> 7

This function is multibyte-reliable.

INSTR (STR,SUBSTR)
Returns the position of the substring substr the first occurrence in string str. This is the same as the locate () with 2 parameter forms, except that the parameters are reversed.
Mysql> Select INSTR (' Foobarbar ', ' Bar ');
-> 4
Mysql> Select INSTR (' Xbar ', ' foobar ');
-> 0

This function is multibyte-reliable.

Lpad (STR,LEN,PADSTR)
Returns the string str, and the left is filled with a string padstr until Str is len characters long.
Mysql> Select Lpad (' Hi ', 4, '?? ');
-> '?? Hi

Rpad (STR,LEN,PADSTR)
Returns the string str, and the right is filled with a string padstr until Str is len characters long.
Mysql> Select Rpad (' Hi ', 5, '? ');
-> ' Hi??? '

Left (Str,len)
Returns the leftmost Len character of the string str.
Mysql> Select Left (' Foobarbar ', 5);
-> ' Fooba '

The function is multiple-byte reliable.

Right (Str,len)
Returns the rightmost Len character of the string str.
Mysql> Select Right (' Foobarbar ', 4);
-> ' Rbar '

The function is multiple-byte reliable.

SUBSTRING (Str,pos,len)

SUBSTRING (str from POS for Len)

MID (Str,pos,len)
Returns a substring of Len characters from String str, starting at position pos. The variant form using from is the ANSI SQL92 syntax.
Mysql> Select SUBSTRING (' quadratically ', 5,6);
-> ' Ratica '

The function is multiple-byte reliable.

SUBSTRING (Str,pos)

SUBSTRING (str from POS)
Returns a substring from the starting position of string Str.
Mysql> Select SUBSTRING (' quadratically ', 5);
-> ' ratically '
Mysql> Select SUBSTRING (' Foobarbar ' from 4);
-> ' Barbar '

The function is multiple-byte reliable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.