Escaping New Line character within SQL query
Escaping New Line character within SQL query
I am reading a table using my java code and creating a csv file out of it. So the 4 rows that I have get converted similar to below -
sam , 18 , banker , He likes to play football
jam , 28 , hacker , he likes nothing
However in certain cases when the last varchar2 column contains n it becomes like this
sam , 18 , banker , He likes
to play
football
jam , 28 , hacker , he
likes
nothing
When I try to read the file , each line is read one at a time and I'm not able parse the data due to few words being pushed to subsequent lines. Is there a way to escape the new line character within the column in my query to make it into a single line?
My sql query
select name , age , job , hobbies from person_details
I am using csvwriter to generate the csv file -
CSVWriter csvWriter = new CSVWriter(new FileWriter(results), DELIMITER, Character.MIN_VALUE, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
I used the below to remove line breaks. However looking for ways to preserve them as well -
replace (replace (hobbies, chr (13), ''), chr (10), ' ')
replace (replace (hobbies, chr (13), ''), chr (10), ' ')
That's not a valid CSV. Maybe you should fix it first using a pre-processor.
– The Impaler
13 hours ago
@JBNizet I am using com.opencsv.CSVWriter to generate the CSV, This is generating the file similar to what is listed above. I used this to convert resultset to csv.
– Punter Vicky
12 hours ago
That's not the output I get when using OpenCSV. It encloses each value into double quotes, unlike what your output shows.
– JB Nizet
11 hours ago
Well, don't do that. Use a CSV parser to parse CSV. OpenCSV also has a CSVReader.
– JB Nizet
11 hours ago
3 Answers
3
Use the REPLACE
or TRANSLATE
functions:
REPLACE
TRANSLATE
SELECT NAME, AGE, JOB, REPLACE(HOBBIES, CHR(10), '')
FROM PERSON_DETAILS
or
SELECT NAME, AGE, JOB, TRANSLATE(HOBBIES, 'x' || CHR(10) || CHR(12) || CHR(13), 'x')
FROM PERSON_DETAILS -- LF FF CR
TRANSLATE
is useful if you have multiple translations to make. Here I've augmented the response to eliminate carriage returns, form feeds, and linefeeds in HOBBIES.
TRANSLATE
Depends on the CSV parser on the other side. They vary a lot in behavior as CSV isn't really standardized. But try to enclose the strings in double quotes.
SELECT '"' || name || '"',
age,
'"' || job || '"',
'"' || hobbies || '"'
FROM person_details;
But JB Nizet's comment has a point. It's probably the best idea to do the transformation in the Java code.
Remove n
while querying. Use the following query.
n
select name , age , job , REPLACE(hobbies,CHR(10),'') "hobbies" from person_details
Oracle doesn't recognize C-style character escapes such as
n
.– Bob Jarvis
10 hours ago
n
I had to use the query in my updated question to remove the line feeds. however will try using csvparser as suggested by @JB Nizet. Thanks for your help!
– Punter Vicky
10 hours ago
@Bobjarvis thanks for pointing that out. Updated the answer
– Pankaj Singhal
1 min ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Google for "Java CSV writer", and pick one of the CSV libraries that will correctly write CSV for you, wherever the data comes from. SQL is irrelevant.
– JB Nizet
13 hours ago