String Case Conversion
MySQL string case conversion functions have two pairs: lower (), Uppper () and LCase (), UCase ()
Mysql> Select lower (' DDD '); +--------------+| Lower (' DDD ') |+--------------+| DDD |+--------------+mysql> Select Upper (' ddd '), +--------------+| Upper (' ddd ') |+--------------+| DDD |+--------------+mysql> Select LCase (' DDD '), +--------------+| lcase (' ddd ') |+--------------+| DDD |+ --------------+mysql> Select UCase (' ddd '); +--------------+| UCase (' ddd ') |+--------------+| DDD |+--------------+
Normally, I choose lower (), upper () to convert the string case, because this is compatible with the functions in other databases.
Clear string and trailing spaces
The purge string in MySQL has three functions: LTrim (), RTrim (), Trim ()
Mysql> Select Concat ('. ', LTrim (' ddd '), '. '); +----------------------------------+| Concat ('. ', LTrim (' ddd '), '. ') |+----------------------------------+|. ddd. | +----------------------------------+mysql> Select Concat ('. ', RTrim (' ddd '), '. '); +----------------------------------+| Concat ('. ', RTrim (' ddd '), '. ') |+----------------------------------+|. DDD. | +----------------------------------+mysql> Select Concat ('. ', Trim (' ddd '), '. '); +---------------------------------+| Concat ('. ', Trim (' ddd '), '. ') |+---------------------------------+|. ddd. | +---------------------------------+
The trim string function in MySQL is really powerful. Not only does it eliminate whitespace from the end of a string, it also eliminates any characters we specify. LTrim (), RTrim () is just a subset of its functions. Take a look at the complete syntax for the TRIM function:
1. Trim ([{both | leading | trailing} [REMSTR] from] str) 2. Trim ([remstr from] str)
1. Clear the string header character.
Mysql> Select trim (Leading ' from '). DDD: '); +----------------------------------+| Trim (leading ' from '). DDD: ') |+----------------------------------+| DDD: | +----------------------------------+
2. Clear the trailing character of the string.
Mysql> Select trim (trailing ' from '). DDD: '); +-----------------------------------+| Trim (trailing ' from '). DDD: ') |+-----------------------------------+| .. DDD |+-----------------------------------+
3. Clear the end of the string character.
Mysql> Select Trim (both ' from '). DDD: '); +-------------------------------+| Trim (both ' from '). DDD: ') |+-------------------------------+| DDD |+-------------------------------+mysql> Select Trim ('. ' From '). DDD: '); +--------------------------+| Trim ('. ' From '). DDD: ') |+--------------------------+| DDD |+--------------------------+
Trim () clears whitespace from the end of the string by default.
String interception
MySQL string intercept function: Left (), right (), substring (), Substring_index (). There is also mid (), substr (). where mid (), substr () is equivalent to the substring () function, the function of substring () is very powerful and flexible.
1. String intercept: Left (str, length)
Mysql> Select Left (' sqlstudy.com ', 3); +-------------------------+| Left (' sqlstudy.com ', 3) |+-------------------------+| SQL |+-------------------------+
2. String intercept: Right (str, length)
Mysql> Select Right (' sqlstudy.com ', 3); +--------------------------+| Right (' sqlstudy.com ', 3) |+--------------------------+| COM |+--------------------------+
3. String interception: Substring (str, POS); SUBSTRING (str, POS, len)
3.1 starts at the 4th character position of the string until the end.
mysql> Select substring (' sqlstudy.com ', 4); +------------------------------+| SUBSTRING (' sqlstudy.com ', 4) |+------------------------------+| study.com |+------------------------------+
3.2 starts at the 4th character position of the string and takes only 2 characters.
mysql> Select substring (' sqlstudy.com ', 4, 2); +---------------------------------+| SUBSTRING (' sqlstudy.com ', 4, 2) |+---------------------------------+| St |+---------------------------------+
3.3 Starts from the 4th character position (reciprocal) of the string until it ends.
mysql> Select substring (' sqlstudy.com ',-4); +-------------------------------+| SUBSTRING (' sqlstudy.com ',-4) |+-------------------------------+|. com |+-------------------------------+
3.4 is taken from the 4th character position (reciprocal) of the string and takes only 2 characters.
mysql> Select substring (' sqlstudy.com ', -4, 2); +----------------------------------+| SUBSTRING (' sqlstudy.com ', -4, 2) |+----------------------------------+|. C |+--------------------------------- -+
We notice that in function substring (str,pos, len), POS can be negative, but Len cannot take a negative value.
4. String interception: Substring_index (Str,delim,count)
4.1 Intercept all characters before the second '. '.
Mysql> Select Substring_index (' www.sqlstudy.com.cn ', '. ', 2); +------------------------------------------------+ | Substring_index (' www.sqlstudy.com.cn ', '. ', 2) |+------------------------------------------------+| Www.sqlstudy |+------------------------------------------------+
4.2 Intercept all characters after the second '. ' (Countdown).
Mysql> Select Substring_index (' www.sqlstudy.com.cn ', '. ',-2); +------------------------------------------------ -+| Substring_index (' www.sqlstudy.com.cn ', '. ',-2) |+-------------------------------------------------+| com.cn |+-------------------------------------------------+
4.3 If the value specified by the Delim parameter is not found in the string, the entire string is returned
Mysql> Select Substring_index (' www.sqlstudy.com.cn ', '. CoC ', 1); +---------------------------------------------- -----+| Substring_index (' www.sqlstudy.com.cn ', '. CoC ', 1) |+---------------------------------------------------+| www.sqlstudy.com.cn |+---------------------------------------------------+
MySQL string function