×

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the string data by using the string function.

MySQL String Function Syntax

MySQL string function syntax gives below.

SELECT string_function("write string here..");

MySQL string function with position syntax shows below.

SELECT string_function("write string here..", position);

MySQL string function with several value syntax shows below.

SELECT string_function("string1", "string2");

MySQL String Functions

The following table shows string functions and its description.

FunctionDescription
ASCIIThe ASCII() function shows the ASCII value of the given character.
CHAR_LENGTHThis function displays the character length. The string contains the character and returns the length of the character, including spaces.
CONCATThe concat() function combines the two or more expressions of the data.
CONCAT_WSThe concat_ws() function is part of the MySQL string function that separates the two or more expressions of the data after concatenation.
FIELDThis function displays the index position of a value in a list.
FIND_IN_SETThe char_length() function displays the position of a value in a list.
INSERTThe INSERT function inserts a string at the required position with a given number of characters.
INSTRThe INSTR function displays the position of the first occurrence of a string with the other strings.
LCASEThe LCASE function uses to convert a string to lower-case. This function returns a string in lower-case.
UCASEThe UCASE function converts a string to upper-case and returns in capital string.
UPPERThe UPPER function is used to convert and shows a given string to the upper-case.
LEFTThe LEFT function extracts several characters from a left string.
LENGTHThe LENGTH function displays the length of a string in bytes format.
LOCATEThe LOCATE function displays the position of the first circumstance of a substring.
LOWERThe LOWER function is used to convert a string to lower-case.
LTRIMThe LTRIM function removes the initial spaces of the string.
MIDThe MID function extracts a substring of a string at any position.
POSITIONThe POSITION function displays the position of the first occurrence of a substring in a string.
REPEATThe REPEAT function repeats a string in the list of the data.
REPLACEThe REPLACE function replaces the circumstance of the substring in a string with a new substring.
REVERSEThe REVERSE function is used to reverses a string and returns the output.
RIGHTThe RIGHT function extracts several characters from the right of the string.
RTRIMThe RTRIM function uses to remove trailing spaces from a string.
SPACEThe SPACE function displays a string of the space characters
STRCMPThe STRCMP function compares two string values and returns the output.
SUBSTRThe SUBSTR function works to remove a substring from a starting string at any position.
TRIMThe TRIM function uses to remove leading and trail spaces from a string.

String Function Examples

MySQL string function works on the database information related string. You can find the length of the string, display string data in the required format, and operate string data as per applications. This function provides multiple operators to modify strings like concat, trim, reverse, and so on.

1. Example of the simple string function

This example shows basic operations about numbers and characters. Here, we have used the four-string functions and display the respective output.

mysql> select reverse("MySQL Tutorial Online") AS reverse,
      UCASE("MySQL Tutorial Online") AS upper_case,
      LCASE("MySQL Tutorial Online") AS lower_case,
      char_length("MySQL Tutorial Online") AS length;

Output

MySQL String Function

We can see four columns for four basic string functions with the "MySQL Tutorial Online" string value. The first column shows the reverse string and the second column displays the upper case string. The third column returns a lower case string, and the fourth column shows character length with space.

2. Example of the string function with multiple values

You can use several string functions in the MySQL query. The following function displays the left, right, and mid-value of the entire string data. Here, you can use the position function to get the place of the required character.

mysql> select left("MySQL Tutorial Online", 6) AS left_character,
       right("MySQL Tutorial Online", 6) AS right_character,
       mid("MySQL Tutorial Online", 7, 8) AS middle_character,
       position( "QL" IN "MySQL Tutorial Online") AS place_of_character;

Output

MySQL String Function

The above image shows the different characters of the string. The left function shows the first six characters, and the right function shows the last six characters of the string. The mid function shows the middle character of the string. The position function returns the available position of the first string in the second string.

3. Example of the string function with data

MySQL string function uses several functions to exchange, replace, and insert information. Here, you use the insert, replace, concat, and substring function with data. The "insert" function exchanges substring using position. MySQL substring function returns part of the string using position.

mysql> SELECT INSERT("MySQL Tutorial Online", 1, 5, "Database") AS insertdata,
     REPLACE("MySQL Tutorial Online", "mysql", "Java") AS exchange,
     CONCAT ("mysql", "online", "tutorial") AS combine,
     SUBSTRING("MySQL Tutorial Online", 7, 8) AS stringpart;

Output

MySQL String Function

Here, the MySQL output image returns four columns for four functions. The insert function exchanges substring from "MySQL" to "database" value. The replace function exchanges substring from "MySQL" to "java" value. The third function combines all string parts. The last function returns the required part of the string data.

4. Example of the string functions

This string function uses trim and RPAD and LPAD functions in the example. The trim function removes space on the left and right sides of the string. Here, the MySQL query uses RPAD and LPAD functions to add value to a string.

mysql> SELECT RTRIM("MySQL Tutorial Online               ") AS lefttrim,
    LTRIM("          MySQL Tutorial Online") AS righttrim,
    RPAD("MySQL Tutorial", 19, "learning") AS rightstring,
    LPAD("MySQL Tutorial", 19, "learning") AS leftstring;

Output

MySQL String Function

Here the trim function removes the right side of the space in the string. The ltrim function avoids left string space. The rpad and lpad functions insert specific data on the right and left sides, respectively.


Related Topics

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL PERIOD_ADD() function

In this context, we will learn how we can use the MySQL PERIOD_ADD() function with proper syntax and good examples. Introduction of MySQL PERIOD_ADD() function In MySQL, PERIOD_ADD() function will help to...

2 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.