MySQL REPEAT Function

The MySQL REPEAT function is one of the String Functions, which is helpful to repeat the given string for user-specified times. This MySQL repeat string accepts both character string or integers and returns the repeated string.

MySQL String Repeat Syntax

The basic syntax of the string Repeat in MySQL is as shown below:

SELECT REPEAT (Expression, int_Expression)
FROM Source
  • Expression: Please specify the valid expression. Repeat function repeats this Expression. It can be a character string or integer.
  • int_Expression: Please specify how many characters you want to repeat the Expression.

To demonstrate this MySQL string repeat, we are going to use the customer details table data that we showed below

MySQL String Repeat Function Example

MySQL Repeat Example 1

The String Repeat Function in MySQL repeats the original string with the specified number of times. The following query shows multiple ways to use this String repeat function.

-- MySQL String Repeat Function Example
SELECT REPEAT('Tutorial', 3);

SELECT REPEAT('MySQL ', 4);

-- Let me Try with Negative value
SELECT REPEAT('Tutorial', -1);

SELECT REPEAT(1214, 4);

-- Testing NULLS
SELECT REPEAT(NULL, 3);
SELECT REPEAT('Tutorial', NULL);
MySQL REPEAT String Function Example 1

MySQL Repeat Function Example 2

In this String Function example, we are going to implement the MySQL string repeat function on First_Name columns. Along with this, we are using the Concat, and Left function to create Serial Number column in MySQL (which is an Alias Column name)

-- MySQL String REPEAT Example
USE company;
SELECT  First_Name, 
		Last_Name,
        CONCAT(First_Name, REPEAT('0', 2), LEFT(Yearly_Income, 2)) AS `Searial Number`,
        Education,    
        Profession, 
        Yearly_Income,      
        Sales
FROM customerdetails

From the screenshot below, you can see we are combining First_Name, 0 (repeated twice), and the first two characters from yearly income.

MySQL REPEAT String Function Example 2