Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REPEAT function equivalent in Oracle

Tags:

sql

oracle

I would like to know how to achieve the same functionality as REPEAT() in SQL*Plus. For example consider this problem: display the character '*' as many times as the value specified by an integer attribute specified for each entry in a given table.

like image 312
GodOfJAR Avatar asked Aug 07 '15 18:08

GodOfJAR


People also ask

Can we use loop in Oracle?

In Oracle, the FOR LOOP allows you to execute code repeatedly for a fixed number of times.

What is the equivalent of datediff in Oracle?

I need the Oracle equivalent to the SQL Server DATEDIFF function to compute the difference between two dates. Answer: Oracle supports date arithmetic and you can make expressions like "date1 - date2" using date subtraction to get the difference between the two dates.

What is the Replace function in Oracle?

An Oracle REPLACE Function is used to replace the string with a given string as the name suggests. It accepts a search string and replaces it with another given string but pattern-wise. It returns CHAR with every replaced with replacement string for the search string.

What is the use of RPAD in Oracle?

RPAD returns expr1 , right-padded to length n characters with expr2 , replicated as many times as necessary. This function is useful for formatting the output of a query.

Can we use datediff in Oracle?

Use the @DATEDIFF function to calculate the difference between two dates or datetimes, in days or seconds. The difference between the specified dates. Valid values can be: DD , which computes the difference in days.

How do I loop a stored procedure in Oracle?

“FOR LOOP” statement is best suitable when you want to execute a code for a known number of times rather than based on some other conditions. In this loop, the lower limit and the higher limit will be specified and as long as the loop variable is in between this range, the loop will be executed.


2 Answers

Nitpicking: SQL*Plus doesn't have any feature for that. The database server (Oracle) provides the ability to execute SQL and has such a function:

You are looking for rpad()

select rpad('*', 10, '*')
from dual;

will output

**********

More details can be found in the manual: https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions159.htm#SQLRF06103

like image 164
a_horse_with_no_name Avatar answered Oct 05 '22 20:10

a_horse_with_no_name


For single characters, the accepted answer works fine.

However, If you have multiple characters in a given string, you need to use RPAD along with length function like this.

WITH t (str) AS
  ( 
   SELECT 'a'
   FROM DUAL
   UNION ALL SELECT 'abc'
   FROM DUAL
   UNION ALL SELECT '123'
   FROM DUAL
   UNION ALL SELECT '#+-'
   FROM DUAL
  )
SELECT RPAD(str, 5*LENGTH(str), str) repeated_5_times
FROM t;

Output:

REPEATED_5_TIMES
---------------
aaaaa
abcabcabcabcabc
123123123123123
#+-#+-#+-#+-#+-
like image 23
Kaushik Nayak Avatar answered Oct 05 '22 21:10

Kaushik Nayak