SQL Oracle 中的 REPEAT 函数等效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31884233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:54:28  来源:igfitidea点击:

REPEAT function equivalent in Oracle

sqloracle

提问by GodOfJAR

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.

我想知道如何在 SQL*Plus 中实现与 REPEAT() 相同的功能。例如,考虑这个问题:显示字符 '*' 的次数与为给定表中的每个条目指定的整数属性指定的值一样多。

回答by a_horse_with_no_name

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:

挑剔:SQL*Plus 没有任何功能。所述数据库服务器(Oracle)的提供执行SQL的能力,并且具有这样的功能:

You are looking for rpad()

你正在寻找 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

更多细节可以在手册中找到:https: //docs.oracle.com/cd/E11882_01/server.112/e41084/functions159.htm#SQLRF06103

回答by Kaushik Nayak

For single characters, the accepted answer works fine.

对于单个字符,接受的答案工作正常。

However, If you have multiple characters in a given string, you need to use RPADalong with lengthfunction like this.

但是,如果给定字符串中有多个字符,则需要RPAD与这样的length函数一起使用。

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
#+-#+-#+-#+-#+-