The MySQL REPEAT function is one of the String Functions, which is helpful for repeating the given string for user-specified times. This repeat method 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 character or an integer.
- int_Expression: Please specify how many characters you want to replicate the Expression.
To demonstrate this MySQL string repeat function, we will use the customer details table data we showed below.
MySQL Repeat Function Example
The Repeat Function 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 the First_Name columns. Along with this, we are using the Concat and Left functions 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.