REPEAT() function in MySQL
Last Updated :
09 Dec, 2020
REPEAT() :
This function in MySQL is used to repeat a string a specified number of times.
Syntax :
REPEAT(str, count)
Parameters :
This method accepts two parameter.
- str –Input String which we want to repeat.
- count –It will describe that how many times to repeat the string.
Returns :
It returns a repeated string.
Example-1 :
Repeating the String ‘Geeks’ 3 times with the help of REPEAT Function.
SELECT REPEAT("Geeks", 3)
AS Repeated_String;
Output :
REPEATED_STRING |
GeeksGeeksGeeks |
Example-2 :
Repeating the String ‘SQL’ 0 times with the help of REPEAT Function.
SELECT REPEAT("SQL", 0)
AS Repeated_String;
Output :
Example-3 :
REPEAT Function can also be used to repeat column data. To demonstrate create a table named Employee.
CREATE TABLE Employee
(
Employee_id INT AUTO_INCREMENT,
Employee_name VARCHAR(100) NOT NULL,
Joining_Date DATE NOT NULL,
PRIMARY KEY(Employee_id )
);
Inserting some data to the Employee table :
INSERT INTO Employee
(Employee_name, Joining_Date )
VALUES
('Ananya ', '2000-01-11'),
('Anush ', '2002-11-10' ),
('Aniket ', '2005-06-11' ),
('Anika ', '2008-01-21' ),
('Riyag ', '2008-02-01' ) ;
So, the Employee Table is as follows.
select * from Employee ;
Output :
EMPLOYEE_ID |
EMPLOYEE_NAME |
JOINING_DATE |
1 |
Ananya |
2000-01-11 |
2 |
Anush |
2002-11-10 |
3 |
Aniket |
2005-06-11 |
4 |
Anika |
2008-01-21 |
5 |
Riyag |
2008-02-01 |
Now, we are going to get all repeated string from Employee_name column.
SELECT REPEAT(Employee_name, 2)
AS Repeated_Name
FROM Employee;
Output :
REPEATED_NAME |
Ananya Ananya |
Anush Anush |
Aniket Aniket |
Anika Anika |
Riyag Riyag |
Share your thoughts in the comments
Please Login to comment...