Return the input string repeated n times.
REPEAT(str, n)
## Overview Returns the result of concatenating the input string with itself `n` times. REPEAT is useful for generating separator lines, producing test fixtures of a known length, and building indented text output for reports. For large repetition counts or for constructing huge strings, consider whether a single CONCAT or an aggregate is more appropriate. The result length grows linearly with `n` and the cost is proportional to the output size. ## Behavior - Returns NULL when any argument is NULL. - If `n` is 0 or negative, returns an empty string. - If the input string is empty, returns an empty string regardless of `n`. - Operates on Unicode code points; multi-byte characters are preserved intact. - No upper bound is enforced on the result size, but very large repetitions consume memory proportional to the output. - Equivalent to SPACE(n) when the input is a single space character. ## Compatibility - Widely supported across SQL dialects with matching semantics.
| Name | Type | Description |
|---|---|---|
str | Specifies the string to repeat. | |
n | Specifies the number of times to concatenate the string. Zero or negative values yield an empty string. |
-- Repeat three times
SELECT REPEAT('ab', 3); -- 'ababab'
-- Zero repetitions returns empty
SELECT REPEAT('hello', 0); -- ''
-- Negative count returns empty
SELECT REPEAT('x', -2); -- ''
-- NULL propagates
SELECT REPEAT(CAST(NULL AS VARCHAR), 3); -- NULL
-- Build a separator row for a text report
SELECT REPEAT('-', 40) AS divider;
-- Generate test strings of a target length
SELECT REPEAT('x', 256) AS long_test_string;