Skip to content

Latest commit

 

History

History
85 lines (64 loc) · 7.63 KB

MySQL String Functions.md

File metadata and controls

85 lines (64 loc) · 7.63 KB

原文地址:https://www.mysqltutorial.org/mysql-string-functions/

MySQL String Functions

This page shows you the most commonly used MySQL string functions that allow you to manipulate character string data effectively.

Name Description
CONCAT Concatenate two or more strings into a single string.
CONCAT_WS Return a single string by concatenating multiple strings separated by a specified separator.
INSTR Return the position of the first occurrence of a substring in a string.
LENGTH Get the length of a string in bytes.
CHAR_LENGTH Return the length of a string measured in characters.
LEFT Get a specified number of leftmost characters from a string.
LOWER Return a string converted to lowercase.
LOCATE Return the position of a substring within a given string starting at a specified position.
LTRIM Remove all leading spaces from a string.
REPLACE Replace all occurrences of a substring in a string with a new one.
REPEAT Repeat a string a specified number of time.
REVERSE Reverse a string.
RIGHT Get a specified number of rightmost characters from a string.
RTRIM Remove all trailing spaces from a string.
SUBSTRING Extract a substring starting from a position with a specific length.
SUBSTRING_INDEX Return a substring from a string before a specified number of occurrences of a delimiter.
TRIM Remove unwanted characters from a string.
FIND_IN_SET Find a string within a comma-separated list of strings.
FORMAT Format a number with a specific locale, rounded to the number of decimals.
UPPER Convert a string to uppercase.

Concatenation Functions

  • CONCAT(): Combines two or more strings into a single string.
  • CONCAT_WS(): Combines multiple strings into a single string with a specified separator.

Substring Functions

  • SUBSTRING(): Extracts a substring from a given string.
  • SUBSTRING_INDEX(): Extracts a substring from a string using a delimiter.
  • LEFT(): Returns a specified number of characters from the beginning of a string.
  • RIGHT(): Returns a specified number of characters from the end of a string.
  • MID(): Extracts a substring from the middle of a string. The MID() function is a synonym for SUBSTRING().

Searching and Locating Functions

  • LOCATE(): Finds the position of a substring within a string.
  • POSITION(): Finds the position of a substring. The POSITION() is a synonym for the LOCATE() function.
  • INSTR(): Another function for finding the position of a substring.

Case Conversion Functions

  • UPPER(): Converts a string to uppercase.
  • LOWER(): Converts a string to lowercase.

Character Manipulation Functions

  • REPLACE(): Replaces all occurrences of a substring in a string.
  • TRIM(): Removes leading and trailing spaces from a string.
  • LTRIM(): Removes leading spaces from a string.
  • RTRIM(): Removes trailing spaces from a string.
  • REPEAT(): Repeats a string a specified number of times.
  • REVERSE(): Reverses the characters in a string.
  • INSERT(): Replaces a substring within a string with a new substring.

Whitespace Functions

  • SPACE(): Returns a string consisting of spaces.
  • ASCII(): Returns the ASCII value of the leftmost character of a string.
  • CHAR(): Converts an ASCII value to a character.

Length and Count Functions

  • LENGTH(): Returns the length of a string in bytes.
  • CHAR_LENGTH(): Returns the length of a string in characters.
  • OCTET_LENGTH(): Returns the length of a string in bytes.
  • BIT_LENGTH(): Returns the length of a string in bits.
  • CHARACTER_LENGTH(): Returns the length of a string in characters.
  • BIT_COUNT(): Counts the number of bits in a binary string.
  • STRCMP(): Compares two strings and returns their relative order.

Padding string functions

  • LPAD() – Left-pads a string with a set of characters to a specified length.
  • RPAD() – Right-pads a string with a set of characters to a specified length.