Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Finding the new line character in a field

PandeeshMay 12 2011 — edited May 18 2011
Hi,

i am cretaing the table as given below:

CREATE TABLE ITEM_TEST(item_desc varchar2(48));

table created

I am inserting the below records

1)insert into ITEM_TEST values('AWT_SWING');

2)insert into ITEM_TEST values('AWT-
SWING');

3)insert into ITEM_TEST values('AWT-S
SWING');

Now there are 3 records in the ITEM_TEST table.

i want to fetch only the records which are in multi line.(i.e 2 and 3 rd records in tis case)

So how should my WHERE clause look like?

i have tried using CHR(10) with LIKE clause.
But i am not getting the desired output,.

Any help would be highly appreciated.

thanks
This post has been answered by Hoek on May 12 2011
Jump to Answer

Comments

Hoek
Answer
Welcome to the forum and thanks for posting sample data!

try INSTR:
SQL> select *
  2  from   item_test;

ITEM_DESC
------------------------------------------------
AWT_SWING
AWT-
SWING

AWT-S
SWING


3 rows selected.

SQL> select *
  2  from   item_test
  3  where instr(item_desc, chr(10)) > 0;

ITEM_DESC
------------------------------------------------
AWT-
SWING

AWT-S
SWING


2 rows selected.
Marked as Answer by Pandeesh · Sep 27 2020
Rafu
select dump(item_desc) from item_test;
Are you actually storing chr(10)
MichaelS
i have tried using CHR(10) with LIKE clause.
But i am not getting the desired output,.
What are you doing differently:
SQL> select *
  from item_test
 where item_desc like '%' || chr (10) || '%'
/
ITEM_DESC                                                               
------------------------------------------------------------------------
AWT-                                                                    
SWING                                                                   
                                                                                                                                  
AWT-S                                                                   
SWING                                                                   
                                                                                                                                  

2 rows selected.
?
Pandeesh
Thanks!!!

SELECT * FROM ITEM_TEST WHERE dump(item_desc) like '%10,%'

gives the required output..
MichaelS
gives the required output..
but it would fail for sth like
SQL> with t as (
  select chr(90) || chr(110) || chr(100) str from dual
)
--
--
select str from t where dump(str) like '%10,%'
/
STR 
----
Znd 
1 row selected.
;)
Pandeesh
one more query in the same.
Pandeesh
One more clarification...

now i am going to inserr one more record:

insert into item_test values('ZAWS-
SWING-
JAVA');

In this case teh field contains 3 lines.

Now i want to retrieve only the multi line records which contains only 2 lines excluding the recently inserted one since it contains 3 lines.

Thanks
Pandeesh
I think the below will satisfy the requirement:

SELECT * FROM ITEM_TEST WHERE DUMP(ITEM_DESC) LIKE '%,10,%'

Edited by: 858433 on May 12, 2011 6:32 AM
user477000
To calculate no of EOL in field compare original field length with the value where EOL is replaced with null

with aa as
(select ' dasda
;
;' as mytext
from dual
union all
select 'texts
' from dual
)
, outp as
(
select aa.*, length(aa.mytext) len_a, length(replace(aa.mytext,chr(10),''))as len_b
from aa
)
select mytext, len_a-len_b no_of_eol
from outp
MichaelS
I think the below will satisfy the requirement:
No:
SQL> with t as (
  select chr(90) || chr(110) || chr(10) str from dual
)
--
--
select str from t where dump(str) like '%,10,%'
/
no rows selected.
I'd still go for either INSTR or LIKE '%' || CHR(10) || '%' ;
Pandeesh
user477000 wrote:
To calculate no of EOL in field compare original field length with the value where EOL is replaced with null

with aa as
(select ' dasda
;
;' as mytext
from dual
union all
select 'texts
' from dual
)
, outp as
(
select aa.*, length(aa.mytext) len_a, length(replace(aa.mytext,chr(10),''))as len_b
from aa
)
select mytext, len_a-len_b no_of_eol
from outp
Can you please elobarate according to my sample table and records?

Thanks
Pandeesh
MichaelS wrote:
I think the below will satisfy the requirement:
No:
SQL> with t as (
select chr(90) || chr(110) || chr(10) str from dual
)
--
--
select str from t where dump(str) like '%,10,%'
/
no rows selected.
I'd still go for either INSTR or LIKE '%' || CHR(10) || '%' ;
Then what about

select str from t where dump(str) like '%,10%'

it will give right?
William Robertson
(Missed something obvious)

Edited by: William Robertson on May 13, 2011 8:40 AM
Pandeesh
What has '%,10%' got to do with it? I thought you were looking for newlines? '%,10%' will find values containing a comma followed by the number 10.
dump(str) like'%,10%' will give the multiline fields since 10 is the asci value for new line character.
836082
While I like the attempts to use DUMP (It's one of my favorite documented functions to pull out when I am at database cocktail parties -- I really livens up the evening!), I don't think that DUMP works here. The problem is that the models are relying on the presence of a comma immediately before and after the "10" in order to make sure that it does not also pull in other character with three digits, two of which happen to be "10" ( for example, Character 105 is lower case "i" and 210 is capital "O" with oblique accent). But, by requiring that those commas surround the "10" in the output from DUMP, you are excluding records where the newline character is either first or last.

I have faced many, many challenges from imported data where a chr(10) or a chr(13) was the very last character in the string! It is vey common.

As for the question of finding those cases with one line feed but not two, if you are on 11g, you can do it very easily with REGEXP_COUNT.
WHERE REGEXP_COUNT ( myfieldvalue, CHR(10)) = 1
William Robertson
Apologies, I totally missed that it was checking the output of DUMP(). It was late...
807800
You can keep on adding to the like clause to check for multiple occurrences.
select * from item_test where item_desc like '%' || chr(10) || '%' || chr(10) || '%'
;
will check for strings containing 2 new lines, you could exclude items with 2 lines by simple saying not like.
Pandeesh
>
>
Then what about

select str from t where dump(str) like '%,10%'

it will give right?
This will also fail for the case :
with t as (
select chr(90) || chr(110) || chr(100) str from dual
)
select str from t where dump(str) like '%,10%'

Znd

1 row returned.

So, the refined one is ,


with t as (
select chr(90) || chr(110) || chr(100) str from dual
*)*
select str from t where dump(str) like '%,10%' and Not ( dump(str) like '%100%')
No rows returned..

Let me know if this fails for any case
Pandeesh
WHERE REGEXP_COUNT ( myfieldvalue, CHR(10)) = 1
But we are using Oracle 8i where there are no regular expression in built functions like REGXP_COUNT
MichaelS
Let me know if this fails for any case
Why do you insist in using DUMP?
Again your query would fail for charriage return at the beginning of the string:
SQL> with t as (
  select chr(10) || chr (90) || chr (110) || chr (100) str from dual
)
--
--
select str
  from t
 where dump (str) like '%,10%'
   and not (dump (str) like '%100%')
/
no rows selected.
Pandeesh
SQL> with t as (
select chr(10) || chr (90) || chr (110) || chr (100) str from dual
)
--
--
select str
from t
where dump (str) like '%,10%'
and not (dump (str) like '%100%')
/
no rows selected.
Thanks Michael!!

Please let me know about the below one:

with t as (
select chr(10) || chr(110) || chr(100) str from dual
)
select str from t where (dump(str) like '%,10%') or (dump(str) like '%10,%') and Not ( dump(str) like '%100%')

nd

1 row selected

let me know if it fails for any case
MichaelS
let me know if it fails for any case
again: why are you insisting on DUMP???

No row should be returned in the following:
SQL> with t as (
  select chr(110) || chr(110) str from dual
)
--
--
select str from t where (dump(str) like '%,10%') or (dump(str) like '%10,%') and Not ( dump(str) like '%100%')
/
STR
---
nn 
1 row selected.
Pandeesh
again: why are you insisting on DUMP???

No row should be returned in the following:



just i want to find the solution with dump..

what about the below?

with t as (
select chr(210) || chr(110) str from dual
)
select str from t where (dump(str) like '%,10%') or (dump(str) like '%10,%') and Not ( dump(str) like '%100%') and Not ( dump(str) like '%110%')

No rows returned

Is it not at all possible with dump?

thanks
Pandeesh
Nigel Ren wrote:
You can keep on adding to the like clause to check for multiple occurrences.
select * from item_test where item_desc like '%' || chr(10) || '%' || chr(10) || '%'
;
will check for strings containing 2 new lines, you could exclude items with 2 lines by simple saying not like.
But
select * from item_test where item_desc like '%' || chr(10) || '%' || chr(10) || '%'

will return the records which consist of 2 or more new line characters.

but how i can make this to return the records with exactly 2 new line charcters and not more than that?

Thanks

Edited by: Pandeesh on May 13, 2011 2:15 AM
MichaelS
Is it not at all possible with dump?
Probably it should be like
...
 where dump (str) like '%,10,%'
    or dump (str) like '% 10,%'
    or dump (str) like '%,10'
807800
The idea is as extensible as you want - just add something like...
select * from item_test where item_desc like '%' || chr(10) || '%' || chr(10) || '%'
       and item_desc not  like '%' || chr(10) || '%' || chr(10) || '%' || chr(10) || '%'
;
MichaelS
but how i can make this to return the records with exactly 2 new line charcters and not more than that?
select *
  from table1
 where length (col) - length (replace (col, chr (10))) = 2
/
Pandeesh
MichaelS wrote:
but how i can make this to return the records with exactly 2 new line charcters and not more than that?
select *
from table1
where length (col) - length (replace (col, chr (10))) = 2
/
Hi Michael,

Can u pls explain with sample data?

Thanks
MichaelS
Can u pls explain with sample data?
SQL> with t as (
  select 'Some text with one' || chr(10) || 'carriage return' str from dual union all
  select 'Some text ' || chr(10) || ' with two'   || chr(10) || 'carriage return' str from dual union all
  select 'Some text ' || chr(10) || ' with three' || chr(10) || 'carriage '||chr(10)||' return' str from dual
)
--
--
select * from t
 where length (str) - length (replace (str, chr (10))) = 2
/
STR                                                         
------------------------------------------------------------
Some text                                                   
 with two                                                   
carriage return                                             
                                                                                
1 row selected.
Pandeesh
Hi,

Recently i have thought about another scenario::

For example i am inserting the below record:;

INSERT INTO ITEM_TEST values('AB
');

1 row inserted..

But i dont want to dispaly this record while selecting.

Even though there is a CHR(10) in this record, there are no characters followed by CHR(10).

Now i am inserting another record

INSERT INTO ITEM_TEST values('AB
CD');

But i want to display this since there are some characters following by CHR(10).

Finally i achieve this by the below queries:

select * from item_test where item_desc like '%' || CHR(10) || '%' and

LENGTH(SUBSTR(ITEM_DESC,INSTR(ITEM_DESC,CHR(10),1,1))) > 1

select * from item_test where item_desc like '%' || CHR(10) || '%' and

SUBSTR(ITEM_DESC,INSTR(ITEM_DESC,CHR(10),1,1)) != CHR(10)

Let me know your comments and please share if you have any other way to achieve this.

Thanks for your support!!

Edited by: Pandeesh on May 16, 2011 12:47 AM

Edited by: Pandeesh on May 16, 2011 12:49 AM
Vivek L
like this?
SELECT *
FROM ITEM_TEST 
WHERE instr(ITEM_DESC,chr(10))>0
AND SUBSTR(ITEM_DESC,LENGTH(ITEM_DESC)-1,1) != chr(10)
Vivek L
Pandeesh
yes Vivek!! Gud!
1 - 32
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 15 2011
Added on May 12 2011
32 comments
31,282 views