Skip to main content

repeat

1. REPEAT Function

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

2. Syntax

The syntax for the REPEAT function in H2 database is as follows:

REPEAT(string, count)

Arguments

  • string: The string that needs to be repeated. It should be a VARCHAR or NVARCHAR value.
  • count: The number of times the string should be repeated. It should be an INTEGER value.

Return

  • The REPEAT function returns a string that is the concatenation of the input string repeated the specified number of times.

3. Notes

  • The REPEAT function in H2 database can be used with any string value, including empty strings.
  • If the input count is negative or zero, the REPEAT function will return an empty string.
  • If the input string is NULL, the REPEAT function will return NULL.
  • Make sure to use the correct datatypes for the string and count arguments to avoid errors.

4. Examples

Here are a few examples demonstrating the usage of the REPEAT function in H2 database:

Example 1 - Repeating a string three times:

SELECT REPEAT('Hello', 3) AS repeated_string;

Output:

repeated_string
---------------
HelloHelloHello

Example 2 - Repeating an empty string:

SELECT REPEAT('', 5) AS repeated_string;

Output:

repeated_string
---------------

Example 3 - Repeating a string stored in a column:

CREATE TABLE strings (
id INT PRIMARY KEY,
value VARCHAR(10)
);

INSERT INTO strings VALUES (1, 'abc'), (2, '123'), (3, 'xyz');

SELECT id, value, REPEAT(value, 2) AS repeated_string
FROM strings;

Output:

id | value | repeated_string
---+-------+----------------
1 | abc | abcabc
2 | 123 | 123123
3 | xyz | xyzxyz
  • lpad - Left pad a string with another string
  • rpad - Right pad a string with another string