PostgreSQL – REPEAT Function

The REPEAT function is used to repeat a string a specified number of times and concatenate the repetitions together to form a longer string. It takes two arguments: the string you want to repeat and the number of times you want to repeat it.

Here’s the basic syntax of the REPEAT function:

REPEAT(string, count)
  • string is the string that you want to repeat.
  • count is the number of times you want to repeat the string.

Here’s an example of using the REPEAT function:

SELECT REPEAT('Hello ', 3); -- Returns 'Hello Hello Hello '

In this example, the REPEAT function repeats the string ‘Hello ‘ three times, and the result is ‘Hello Hello Hello ‘.

You can use the REPEAT function with column values in a table as well. For instance, if you have a table named employees with a column named name and you want to repeat each star name five times, you can do it like this:

SELECT REPEAT(name, 5) FROM employees;

This query will return a result set with each name repeated five times in the output.

The REPEAT function is handy when you need to generate repetitive strings or padding characters for formatting purposes in your SQL queries.

Similar Posts