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 accepts both characters or integers and returns the repeated string and the basic syntax of it is as shown below:
SELECT REPEAT(Expression, int_Expression) FROM Source
- Expression: Please specify the valid expression. it repeats this Expression. It can be a characters or integer.
- int_Expression: Please specify how many characters you want to replicate the Expression.
To demonstrate this MySQL string repeat, we are going to use the customer details table data that we showed below
MySQL Repeat Function Example
The Repeat Function in MySQL replicates the original string with the specified number of times. The following query shows multiple ways to use this method.
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);
In this method example, we are going to implement it on First_Name columns. Along with this, we are using the Concat, and Left function to create a Serial Number column in MySQL. And it is an Alias Column name.
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.