简体   繁体   English

如何在Oracle中一起使用set null和concatenate运算符?

[英]how to use set null and concatenate operator together in Oracle?

I am trying to export data from Oracle into CSV with the below commands: 我正在尝试使用以下命令将数据从Oracle导出到CSV:

SET ECHO OFF 
SET NEWPAGE 0 
SET SPACE 0 
SET PAGESIZE 0 
SET FEED OFF
SET HEAD OFF
SET null NULL
SPOOL STUDENT.CSV
SELECT ID ||’,’|| NAME ||’,’|| ADMISSION_DATE FROM STUDENT; 
SPOOL OFF;

However, when using the concatenate operator(||','||) and set null NULL option together, it keeps the null value as a blank string ('') in CSV file instead of replacing it with NULL. 但是,当使用连接运算符(||','||)并一起设置null NULL选项时,它将NULL值保留为CSV文件中的空白字符串(''),而不是将其替换为NULL。

Can someone please tell how to use set null and concatenate operator together? 谁能告诉我如何一起使用set null和concatenate运算符?

SQL*Plus commands (like set null ) can't help in this case, since the null value is already gone by the time SQL*Plus can see the data. 在这种情况下,SQL * Plus命令(如set null )无济于事,因为在SQL * Plus可以看到数据时,null值已经消失了。

set null NULL
select 'a' || null || 'b' from dual; -- returns "ab"

If you want to replace null values, you have to do it before concatenating. 如果要替换空值,则必须连接之前执行此操作。 So you'll need to wrap all of your nullable columns in NVLs, eg 因此,您需要将所有可为空的列包装在NVL中,例如

select 'a' || nvl(null, 'NULL') || 'b' from dual; -- returns "aNULLb"

Alternately, you could try using colsep to put the commas in your CSV , but that adds a bunch of whitespace that you might not want. 或者,您可以尝试使用colsep 将逗号放入CSV ,但这会增加一堆您可能不需要的空格。

set null NULL
set colsep ,
select 'a', null, 'b' from dual; -- returns "a  ,NULL,b  "

If you have the option of switching to Oracle's SQLcl client, it makes it much easier to generate correct CSV files from queries. 如果您可以选择切换到Oracle的SQLcl客户端, 则可以更轻松地从查询生成正确的CSV文件。

set null NULL
select /*csv*/ 'a', null, 'b' from dual; -- returns "a","NULL","b"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM