REPEAT
REPEAT is a string function in SQL that allows a specific string to be repeated a certain number of times. It returns a string that is the result of repeating the input string for a specified number of times.
REPEAT(string, count)
- string: This parameter refers to the string that will be repeated. It can be any string of characters the user wishes to repeat.
- count: This parameter indicates the number of times the specified string should be repeated. If count is less than 1, the function returns an empty string.
Example
SELECT REPEAT('SQL ', 3) AS RepeatedString;
Output
RepeatedString---------------SQL SQL SQL
Explanation
In the given SQL statement, the MySQL REPEAT()
function is used. This function repeats a given string for a specified number of times. Here, ‘SQL ’ is repeated 3 times, producing the output ‘SQL SQL SQL ’.
REPEAT(string, count)
- string: This parameter represents the string that needs to be repeated. It’s a character string expression. It can be a variable, function, or column of either character or binary data.
- count: This parameter indicates the number of times the preceding string should be repeated. It’s of integer data type. Negative values return an empty string, and a NULL value in either parameter returns NULL.
Example
SELECT REPEAT('SQL', 3) AS RepeatedString;
Output
RepeatedString---------------SQLSQLSQL
Explanation
In the above example, The REPEAT()
function in SQL repeats a string a specific number of times. The string ‘SQL’ is repeated 3 times to result in ‘SQLSQLSQL’.
REPEAT(text, count)
- text: This parameter defines the string that needs to be repeated in the database.
- count: This parameter specifies the number of times the string defined in the ‘text’ parameter needs to be repeated.
Example
SELECT REPEAT('A', 5);
Output
'AAAAA'
Explanation
The REPEAT
function in SQL repeats the specified string a certain number of times. In this example, the string ‘A’ is repeated 5 times, which is indicated by the number following the string. The output is a single string of ‘A’s concatenated together.