Oracle RPAD() 填充空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18751882/
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-19 01:55:05  来源:igfitidea点击:

Oracle RPAD() padding with an empty string

sqloracle

提问by vivek ashodha

I am trying to create a dump file from within SQL*Plus. The requirement is to create null '' for padding but when I use NULL even the data value is getting nullified see below.

我正在尝试从 SQL*Plus 中创建转储文件。要求是为填充创建 null '' 但是当我使用 NULL 时,即使数据值也被取消了,见下文。

SQL> select RPAD(1234,10,' ') from dual ;

RPAD(1234,
----------
1234

SQL> select RPAD(1234,10,'') from dual;

R
-

I have seen other scripts where they seem to be using null('') for padding

我看过其他脚本,它们似乎使用 null('') 进行填充

Please help thanks

请帮忙谢谢

回答by Jeffrey Kemp

RPAD accepts a character or string as its 3rd parameter which is used to "pad" the initial string to a particular length.

RPAD 接受一个字符或字符串作为其第三个参数,用于将初始字符串“填充”到特定长度。

RPAD can be used to return a string which is "guaranteed" to be ncharacters long (as per the 2nd parameter).

RPAD 可用于返回“保证”为n 个字符长的字符串(根据第二个参数)。

Since NULL does not represent any particular character or string and has zero length, it cannot be used for padding - RPAD apparently returns NULL in this instance, which makes sense as the only other option would be for RPAD to raise an exception.

由于 NULL 不代表任何特定的字符或字符串并且长度为零,因此它不能用于填充 - 在这种情况下,RPAD 显然返回 NULL,这是有道理的,因为唯一的其他选项是 RPAD 引发异常。

回答by adona9

This code:

这段代码:

RPAD(1234,10,'')

concatenates 1234 to '', which in Oracle is equivalent to NULL, therefore it results in NULL (anything concatenated to NULL yields NULL)

将 1234 连接到 '',这在 Oracle 中相当于 NULL,因此它导致 NULL(连接到 NULL 的任何内容都会产生 NULL)

There is no NULL('') in Oracle.

Oracle 中没有 NULL('')。

Hope that helps.

希望有帮助。