Skip to main content

REPEAT

Description

The REPEAT function is used to repeat a specified string a specified number of times to generate a new string. This function is commonly used for generating padding characters, creating separators, or generating test data.

Syntax

REPEAT(<str>, <count>)

Parameters

ParameterDescription
<str>The source string to repeat. Type: VARCHAR
<count>The number of times to repeat, must be a non-negative integer. Type: INT

Return Value

Returns VARCHAR type, representing the string repeated the specified number of times.

Repeat rules:

  • When count is greater than 0, returns the result of str repeated count times
  • When count equals 0, returns an empty string
  • When count is less than 0, returns an empty string
  • If the resulting string is too long, it may be subject to string length limitations

Special cases:

  • If any parameter is NULL, returns NULL
  • If str is an empty string, returns an empty string regardless of count
  • If count is 0, returns an empty string
  • If count is negative, returns an empty string

Examples

  1. Basic character repetition
SELECT REPEAT('a', 3);
+----------------+
| REPEAT('a', 3) |
+----------------+
| aaa |
+----------------+
  1. Multi-character string repetition
SELECT REPEAT('hello', 2);
+--------------------+
| REPEAT('hello', 2) |
+--------------------+
| hellohello |
+--------------------+
  1. Zero repetitions
SELECT REPEAT('test', 0);
+-------------------+
| REPEAT('test', 0) |
+-------------------+
| |
+-------------------+
  1. Negative repetitions
SELECT REPEAT('a', -1);
+-----------------+
| REPEAT('a', -1) |
+-----------------+
| |
+-----------------+
  1. NULL value handling
SELECT REPEAT(NULL, 3), REPEAT('a', NULL);
+------------------+-------------------+
| REPEAT(NULL, 3) | REPEAT('a', NULL) |
+------------------+-------------------+
| NULL | NULL |
+------------------+-------------------+
  1. Empty string repetition
SELECT REPEAT('', 5);
+----------------+
| REPEAT('', 5) |
+----------------+
| |
+----------------+
  1. Special character repetition
SELECT REPEAT('-', 10), REPEAT('*', 5);
+------------------+-----------------+
| REPEAT('-', 10) | REPEAT('*', 5) |
+------------------+-----------------+
| ---------- | ***** |
+------------------+-----------------+
  1. UTF-8 character repetition
SELECT REPEAT('ṭṛì', 3), REPEAT('ḍḍu', 2);
+-------------------+-------------------+
| REPEAT('ṭṛì', 3) | REPEAT('ḍḍu', 2) |
+-------------------+-------------------+
| ṭṛìṭṛìṭṛì | ḍḍuḍḍu |
+-------------------+-------------------+
  1. Mixed numbers and symbols
SELECT REPEAT('123', 3), REPEAT('@#', 4);
+-------------------+------------------+
| REPEAT('123', 3) | REPEAT('@#', 4) |
+-------------------+------------------+
| 123123123 | @#@#@#@# |
+-------------------+------------------+
  1. UTF-8 character repetition
SELECT REPEAT('ṭṛìṭṛì', 3);
+--------------------------------------------------+
| REPEAT('ṭṛìṭṛì', 3) |
+--------------------------------------------------+
| ṭṛìṭṛìṭṛìṭṛìṭṛìṭṛì |
+--------------------------------------------------+

Description

The REPEAT function is used to repeat a string a specified number of times.

Syntax

REPEAT( <str>, <count> )

Parameters

ParameterDescription
<str>The string to be repeated.
<count>The number of times to repeat. It must be a non-negative integer. If it is less than 1, an empty string will be returned.

Return Value

Returns the string repeated the specified number of times. Special cases:

  • If any Parameter is NULL, NULL will be returned.

Examples

SELECT repeat("a", 3);
+----------------+
| repeat('a', 3) |
+----------------+
| aaa |
+----------------+
SELECT repeat("a", -1);
+-----------------+
| repeat('a', -1) |
+-----------------+
| |
+-----------------+