×

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 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.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL MOD() function

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

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

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 COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 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 RAND() function

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

3 minutes read.

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 ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

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 QUARTER() function

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

3 minutes read.

MySQL LENGTH() Function

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

2 minutes read.