Home » SQL & PL/SQL » SQL & PL/SQL » CR/LR in column (Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 )
| CR/LR in column [message #631622] |
Fri, 16 January 2015 09:22  |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
Anyone know how I might transform data that is broken across two lines in a column and make it into one line of text?
1 PROF & INT DIV HEAD
2 CELL BIOLOGY & BIOPHYSICS
INTO
1 PROF & INT DIV HEAD CELL BIOLOGY & BIOPHYSICS
Insert into TITLE
(TITLE)
Values
('ASSISTANT PROFESSOR');
Insert into TITLE
(TITLE)
Values
('PROFESSOR ADJUNCT');
Insert into TITLE
(TITLE)
Values
('CLINICAL INSTRUCTOR');
Insert into TITLE
(TITLE)
Values
('PROF & INT DIV HEAD
CELL BIOLOGY & BIOPHYSICS');
COMMIT;
Why do I need the data like this? It would appear that JSON doesn't like the data to be in two lines.
{
"title": "ASSISTANT PROFESSOR"
},
{
"title": "PROFESSOR ADJUNCT"
},
{
"title": "CLINICAL INSTRUCTOR"
},
{
"title": "PROF INT DIV HEAD
CELL BIOLOGY BIOPHYSICS"
}
Parse error on line 779:
... "title": "PROF INT DIV HEAD
-----------------------^
|
|
|
|
|
|
| Re: CR/LR in column [message #631625 is a reply to message #631624] |
Fri, 16 January 2015 09:51   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
|
I don't believe there are any rules. I suspect the HR data entry person entering the data hit the enter key resulting in the data being broken across two lines. Sure, this isn't my problem but I highly doubt I can get this fixed and I'm sure it will happen again so that's why I'm trying to handle this situation on my end.
|
|
|
|
|
|
| Re: CR/LR in column [message #631629 is a reply to message #631626] |
Fri, 16 January 2015 10:06   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
Like this?
trim(replace(replace(replace(um_working_title, '&'), chr(10)||chr(13)), '"')) title
That didn't fix the problem either.
|
|
|
|
|
|
| Re: CR/LR in column [message #631631 is a reply to message #631630] |
Fri, 16 January 2015 10:28   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
I was trying to replace the CF/LF. I might have that wrong on the '"' portion.
Anyway, your example doesn't work with my test data.
I used this:
select '"title":"'||replace(title,chr(10))||'"' result from title;
That produced:
1 "title":"PROF & INT DIV HEAD
2 CELL BIOLOGY & BIOPHYSICS"
The result is still broken into 2 lines.
|
|
|
|
|
|
| Re: CR/LR in column [message #631633 is a reply to message #631632] |
Fri, 16 January 2015 10:47   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
I suspect the test data I provided within the post doesn't have a real CF/LF in it. I can verify that the data in my TITLE table has two columns for that data. When I click that column and TOAD brings up the data it is (line 1) PROF & INT DIV HEAD (line 2) CELL BIOLOGY & BIOPHYSICS. A simple test for you would be to make sure your test data has 2 lines and then run your statement.
1 PROF & INT DIV HEAD
2 CELL BIOLOGY & BIOPHYSICS
|
|
|
|
|
|
|
|
| Re: CR/LR in column [message #631636 is a reply to message #631634] |
Fri, 16 January 2015 10:53   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
I tried the data I provided in my post.
CREATE TABLE TITLE_CABOT
(
TITLE VARCHAR2(100 BYTE)
)
Insert into TITLE_CABOT
(TITLE)
Values
('ASSISTANT PROFESSOR');
Insert into TITLE_CABOT
(TITLE)
Values
('PROFESSOR ADJUNCT');
Insert into TITLE_CABOT
(TITLE)
Values
('CLINICAL INSTRUCTOR');
Insert into TITLE_CABOT
(TITLE)
Values
('PROF & INT DIV HEAD
CELL BIOLOGY & BIOPHYSICS');
COMMIT;
select '"title":"'||replace(title,chr(10))||'"' result from title_cabot;
OR EVEN
select replace(title,chr(10)) result from title_cabot;
This still produced a column value of 2 lines.
|
|
|
|
|
|
|
|
| Re: CR/LR in column [message #631639 is a reply to message #631638] |
Fri, 16 January 2015 11:17   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
Ok, you are correct. The result shows one line.
SQL*Plus: Release 11.2.0.3.0 Production on Fri Jan 16 11:01:56 2015
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select replace(title,chr(10)) result from title_cabot
2 /
RESULT
--------------------------------------------------------------------------------
ASSISTANT PROFESSOR
PROFESSOR ADJUNCT
CLINICAL INSTRUCTOR
PROF & DIV HEAD CELL BIOLOGY
SQL> select replace(title,chr(10)) result from title_cabot
Here's what I'm doing and JSON still won't validate it.
JSONRows clob;
for i in (select emplid,
case
when name_middle <> ' '
then
trim(replace(replace(name_last||', '||name_first||' '||name_middle, chr(9)), '"'))
else
trim(replace(replace(name_last||', '||name_first, chr(9)), '"'))
end name,
--trim(replace(replace(replace(um_working_title, '&'), chr(10)||chr(13)), '"')) title,
replace(um_working_title, chr(10)) title,
descr,
phone,
address1 address,
emailid email,
website
from faculty_staff_lu.faculty_staff_dir
inner join faculty_staff_lu.department_name on faculty_staff_lu.faculty_staff_dir.deptid = faculty_staff_lu.department_name.deptid
where faculty_staff_lu.faculty_staff_dir.deptid = upper(Search) and
faculty_staff_lu.faculty_staff_dir.emplid is not null
order by name_last,
name_first)
loop
JSON := '{"name": "'||i.name||'", '||
'"title": "'||i.title||'", '||
'"descr": "'||i.descr||'", '||
'"phone": "'||i.phone||'", '||
'"address": "'||i.address||'", '||
'"email": "'||i.email||'", '||
'"website": "'||i.website||'"},';
JSONRows := JSONRows||JSON;
end loop;
JSONRows := rtrim(JSONRows, ',');
JSON := '{"data": ['||JSONRows||']}';
Produces something like this:
{
"name": "xxxx",
"title": "PROF & INT DIV HEAD
CELL BIOLOGY & BIOPHYSICS",
"descr": "Biology - General",
"phone": "xxxx",
"address": "SCB",
"email": "xxxx",
"website": "xxxx"
},
Parse error on line 779:
... "title": "PROF & INT DIV HEAD
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
|
|
|
|
|
|
| Re: CR/LR in column [message #631641 is a reply to message #631640] |
Fri, 16 January 2015 11:45   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
Will do. Just not sure what to think. It's like the data being pulled out of that column (100 BYTE column) for that one value has a million spaces between HEAD and CELL of the title. I'm guessing some characters that make the lines break for some reason.
{"name": "xxxxx", "title": "PROF & INT DIV HEAD
CELL BIOLOGY & BIOPHYSICS", "descr": "xxxx", "phone": "xxxx", "address": "xxx", "email": "xxxx", "website": "xxxx"},
|
|
|
|
|
|
|
|
| Re: CR/LR in column [message #631644 is a reply to message #631642] |
Fri, 16 January 2015 11:52   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
Got it to work. Looks like the LF character was the problem all along. Added the CHR(13) to the replace and that fixed it. I was trying to do CHR(10)||CHR(13) as a pair. Need to add a space.
replace(replace(um_working_title, chr(10)), chr(13)) title,
{
"name": "xxx",
"title": "PROF & INT DIV HEADCELL BIOLOGY & BIOPHYSICS",
"descr": "xxx",
"phone": "xxx",
"address": "xxx",
"email": "xxx",
"website": "xxx"
},
[Updated on: Fri, 16 January 2015 11:54] Report message to a moderator
|
|
|
|
|
|
| Re: CR/LR in column [message #631646 is a reply to message #631645] |
Fri, 16 January 2015 12:00   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
|
Nope. That fixed the JSON and now the result set is valid. Web site was also broke because of the invalid JSON. Now, the web site works again. I edited my post to show the JSON result and how the broken data is now on one line. I believe TOAD was interpreting the data correctly and showing the data as two lines because of the LF character. That's why the JSON data was showing the title as being broken as in two lines.
|
|
|
|
| Re: CR/LR in column [message #631647 is a reply to message #631643] |
Fri, 16 January 2015 12:15   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
BlackSwan wrote on Fri, 16 January 2015 11:51> I'm guessing some characters that make the lines break for some reason.
Does "line break" REALLY exist in the data or does it get manifested by external tool due to size limitation or similar restriction.
Sort of WYSIWYG in reverse; What You See does not really exist within the DB?
Just asking.
It would appear that an actual LF was in the data.
|
|
|
|
|
|
| Re: CR/LR in column [message #631654 is a reply to message #631649] |
Fri, 16 January 2015 12:35   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
I'm sorry you don't believe me but I honestly only added CHR(13) to what you gave me to use and that indeed did fixed the problem. I did nothing else. The data is exactly the same since I can't change it.
This is the statement I'm using now. I only added the additional REPLACE CHR(13) to what you had shown me. That corrected my JSON issue of the title being broken.
Was this:
{
"name": "xxxx",
"title": "PROF & INT DIV HEAD
CELL BIOLOGY & BIOPHYSICS",
"descr": "Biology - General",
"phone": "xxxx",
"address": "SCB",
"email": "xxxx",
"website": "xxxx"
},
Parse error on line 779:
... "title": "PROF & INT DIV HEAD
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
To this:
replace(replace(um_working_title, chr(10)), chr(13), ' ') title,
{
"name": "xxx",
"title": "PROF & INT DIV HEAD CELL BIOLOGY & BIOPHYSICS",
"descr": "xxx",
"phone": "xxx",
"address": "xxx",
"email": "xxx",
"website": "xxx"
},
[Updated on: Fri, 16 January 2015 12:35] Report message to a moderator
|
|
|
|
| Re: CR/LR in column [message #631656 is a reply to message #631654] |
Fri, 16 January 2015 12:48   |
Solomon Yakobson
Messages: 3312 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
You were almost there. Unfortunately new line in different OS is different. It can be a single line feed character LF, which is chr(10) or carriage return character CR, which is chr(13) or carriage return followed by linefeed - CRLF. Your original code was using LFCR:
Like this?
trim(replace(replace(replace(um_working_title, '&'), chr(10)||chr(13)), '"')) title
So obviously it wouldn't work no matter what type of new line is used in your case. Now, based on the fact replacing just chr(10) didn't work for you and replacing both chr(10) and chr(13) did, I assume new line in your case is CRLF and instead of replacing chr(10) and chr(13) in two separate steps you need just:
trim(replace(replace(replace(um_working_title, '&'), chr(13)||chr(10)), '"')) title
SY.
|
|
|
|
| Re: CR/LR in column [message #631657 is a reply to message #631654] |
Fri, 16 January 2015 12:48   |
 |
Michel Cadot
Messages: 68776 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
No, you replace a problem by another one which is not currently visible but will raise sooner or later.
Let me rephrase it to make you understand.
You are a doctor, a woman comes with her baby (the data and application) and says look my baby has fever, is temperature is high (JSON returns an error).
You take the baby and put him into a bath full of ice (you replace LF by CR).
Few minutes later you tell to the mother, now it is OK the temperature is no more high (JSON no more returns an error).
You say "that's OK, let's go on like that".
...
A short time later, the baby is dead.
How much time it will last before your user will realizes she made actions based on wrong stuff?
But why do you care, JSON no more returns an error.
|
|
|
|
|
|
| Re: CR/LR in column [message #631659 is a reply to message #631657] |
Fri, 16 January 2015 12:59   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
|
I understand what you are saying but I can only fix what I know is wrong. The statement of replace CHR(10) and replace CHR(13) will be there to fix that. If the user then enters in something else, well, I'll fix that also if it breaks the JSON, whatever that might be.
|
|
|
|
|
|
| Re: CR/LR in column [message #631662 is a reply to message #631659] |
Fri, 16 January 2015 13:06   |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
Ok, so it screws the data. I'm altering the data from it's original format. What's the solution? I guess I'm not seeing what you are driving at. I can't change the data. If the user corrected the data it's possible that someone else will do the same thing. If I don't alter the data then the web site breaks.
How would you handle this situation?
|
|
|
|
| Re: CR/LR in column [message #631663 is a reply to message #631660] |
Fri, 16 January 2015 13:10   |
Solomon Yakobson
Messages: 3312 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel,
OP did two REPLACEs:
replace(replace(um_working_title, chr(10)), chr(13), ' ') title
First removes all chr(10) characters and second removes all chr(13) characters. I don't see replacing LF with CR anywhere in OPs code.
SY.
|
|
|
|
|
|
|
|
| Re: CR/LR in column [message #631666 is a reply to message #631665] |
Fri, 16 January 2015 13:40  |
Duane
Messages: 593 Registered: December 2002
|
Senior Member |
|
|
SY,
Yes, I think my original statement would have worked if I flipped the CHR(10)||CHR(13). As you pointed out, they should have been flipped and that was my mistake.
|
|
|
|
Goto Forum:
Current Time: Wed Aug 26 02:25:04 CDT 2026
|