The MySQL REPEAT function is one of the String Function, which is used to repeat the given string for user specified times. This MySQL repeat string function accepts both character string or integers, and returns the repeated string. In this article we will show you, How to write String REPEAT in MySQL with example.
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 will repeat this Expression. This can be character string or integer.
- int_Expression: Please specify, how many characters you want to repeat the Expression.
In order to demonstrate this string repeat function in MySQl, We are going to use the customer details table data that we shown below
MySQL Repeat Example 1
The String Repeat Function in MySQL is used to repeat the original string with specified number of times. The following query will show 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 Function Example 2
In this 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 (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 below screenshot you can we are combining First_Name, 0 (repeated twice), and first two characters from yearly income
Thank You for Visiting Our Blog