0

I am trying to export data from Oracle into CSV with the below commands:

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.

Can someone please tell how to use set null and concatenate operator together?

1 Answers1

0

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.

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, e.g.

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.

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.

set null NULL
select /*csv*/ 'a', null, 'b' from dual; -- returns "a","NULL","b"
kfinity
  • 8,581
  • 1
  • 13
  • 20