Reputation: 107
I have to extract data to a comma delimited file .csv so I'm using a cursor and a for loop that looks like this:
CURSOR l_body IS
SELECT b.brn_code ||','||
t.terminal_no ||','||
t.pos_username ||','||
to_char(t.trn_datetime,'dd-Mon-yyyy') ||','||
to_char(t.trn_datetime,'hh24:mi:ss') ||','||
t.trn_type_code);
FROM tables etc.
and my loop looks like this:
FOR x IN l_body LOOP
utl_file.put_line(out_file_laybye, x.data_line);
END LOOP;
Now say for instance the value in username in my table is 'John Doe,' Is there a way that I can make sure that I remove the comma after that name before I write it to the file using the ||','||?
I don't want to get double commas in my extract. Thanks in advance.
Upvotes: 0
Views: 66
Reputation: 191235
You can use the replace()
function to remove any commas in each value:
select replace(', this, is, a, test,', ',', null) from dual;
REPLACE(',THIS,
---------------
this is a test
You'll need to do it for each column, so
...
replace(t.pos_username, ',', null) ||','||
...
You could also substitute a different character, and you can use translate()
for that too:
select translate(', this, is, a, test,', ',', ' ') from dual;
TRANSLATE(',THIS,IS,
--------------------
this is a test
Depending on where this output is going to be used, you might prefer to keep the commas but escape them somehow. If your CSV will be used in Excel, for example, you can wrap the column value in double-quotes so the commas within a value are not confused with separators:
...
'"' || t.pos_username || '"' ||','||
...
That's useful for something like an address where the commas might be helpful in the output. Of course, you have to make sure the string doesn't contain double-quotes, or escape those somehow, but that was probably already the case.
Upvotes: 4