Extract particular line from CLOB - ORACLE - oracle

I have field in a Oracle database of type CLOB. I extract the first line alone from this CLOB filed. Here's an example of the content:
"<div class="pi-tier3"><div class="pi-pdpmainbody"><p><b>FIT</b></p><p>Core Indy - Compression </p>
<p><b>PRO</b></p><ul>
<li>ABCDEF:PmId12345RmLn1VlId0</li>
<li>ABCDEF:PmId12345RmLn1VlId0</li>
<li>ABCDEF:PmId12345RmLn1VlId0</li>
<li></li>
<li>ABCDEF:PmId12345RmLn1VlId0</li>
</ul>
<p><b>PRP</b></p><ul>
<li>100%</li>
<li>DRY</li>
</ul>
<p>ABCDEF:PmId12345RmLn1VlId0</p>
</div></div>"
The result should look like this:
"<div class="pi-tier3"><div class="pi-pdpmainbody"><p><b>FIT</b></p><p>Core Indy - Compression </p>

If you truly want to return the characters from the beginning of the clob value to the characters before the first newline (chr 10), then here is an example:
create table tab1 (c1 clob);
insert into tab1 values ('line 1' || chr(10) || 'line 2');
select dbms_lob.substr(c1, dbms_lob.instr(c1, chr(10))-1, 1) from tab1;
DBMS_LOB.SUBSTR(C1,DBMS_LOB.INSTR(C1,CHR(10))-1,1)
------------------------------------------------------------------------------
line 1
Check this out for more information.

I would do that using REGEXP_SUBSTR, like so:
select
REGEXP_SUBSTR(
'<div class="pi-tier3"><div class="pi-pdpmainbody"><p><b>FIT</b></p><p>Core Indy - Compression </p>
<p><b>PRO</b></p><ul>'
, '^.*$', 1, 1, 'm')
from dual;
Cheers

Related

Oracle Apex past Value using Dynamic Link Item

So I have a link on my SQL query. It can navigate to the next page. But the thing is, I cannot carry the host_id(PK) to the next page: Anyone suggestion?
Concatenate it.
select
'...:P39_COPY_HOST_ID:' || host_id ||'"...'
from ... -------------
this
Or, even better, use apex_page.get_url function:
select ...
'<a href="' ||
apex_page.get_url(p_page => 39,
p_items => 'P39_COPY_HOST_ID',
p_values => host_id)
|| '"</a>' as link
from ...

Oracle REGEXP_SUBSTR extracts strings by blank when there are multiple blanks

Hi I'm new to Oracle SQL, I want to extract LiIon Polymer from 6Cell LiIon Polymer.
I use REGEXP_SUBSTR('6Cell LiIon Polymer', '\S+', 7) but it only returns LiIon
You want substring that follows the first space? Use good, old substr + instr combination. Sample data first, query you might be interested in begins at line #4.
SQL> with test (col) as
2 (select '6Cell LiIon Polymer' from dual)
3 --
4 select substr(col, instr(col, ' ') + 1) result
5 from test;
RESULT
-------------
LiIon Polymer
SQL>
Using Regular expression and without hardcoding
select REGEXP_SUBSTR('6Cell LiIon Polymer', '[^\S]+', instr('6Cell LiIon Polymer', ' '), 1) from dual;

How to add new line separator into data for oracle

I'm working on displaying data from oracle.
is there a way to make the following data inside the table:
example :
'1.somedata, 2.somedata, 3.somedata, 4.somedata, 5.somedata'
to display like:
example:
'1. somedata
2. somedata
3. somedata
4. somedata
5. somedata'
on the interface?
do i add new line separator directly into the data?
or do i separator them into new line when i query it?
or is there any other simple way?
Thanks.
There are so many ways to do this, here is one if you are selecting from a column:
SELECT REPLACE ('1.somedata, 2.somedata, 3.somedata, 4.somedata, 5.somedata', ',', CHR (13) || CHR (10)) AS split
FROM DUAL;
1.somedata
2.somedata
3.somedata
4.somedata
5.somedata
I personally would use the listagg function and use '' as the delimiter.
SELECT LISTAGG(last_name, ' ')
WITHIN GROUP (ORDER BY hire_date, last_name) "Emp_list",
MIN(hire_date) "Earliest"
FROM employees
WHERE department_id = 30;
Remember that Apex is generating a web page, which means the end result is HTML. Apex, however, will also sometimes escape special HTML characters for you, like < and &. Since you're viewing a table, I assume the source of your data is a query and your "somedata" field is a single column. Try this:
SELECT REPLACE( somedata_column, ',', '<br />' )
FROM mytable
You don't say what version of Apex. In Apex 4.x, the column would need to be set to a Standard Report Column, which would stop Apex from the <br> elements. I forget what the column type is in Apex 5.x.
Check below sample query which converts coma separated list data into rows
SELECT substr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,',
( case when rownum = 1 then 1
else instr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,', ',', 1, rownum - 1 ) + 1
end ),
instr( substr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,',
( case when rownum = 1 then 1
else instr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,', ',', 1, rownum - 1 ) + 1
end )
), ',' ) - 1
) as data
FROM dual
CONNECT BY LEVEL <= length( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,' ) - length ( replace('1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,', ',') )
Hope this will help you!

Using Xpath in Hive

I'm trying to parse some data that is stored in a Hive table.
Let's say.
Hive> SELECT * FROM fulldatatable LIMIT 1;
SELECT xpath( 'xml data from the previous command') SELECT src LIMIT 1;
My question is how to load the first in the xpath query?
Thanks,
You may create a view from the first select, and then query this one with the xpath UDF. E.g:
Initial tables:
hive> describe table1;
id int
f1 string
f2 string
f3 string
f4 string
hive> select * from table1;
1 <a> <b>1</b> <b>1</b> </a>
2 <a> <b>1</b> <b>2</b> </a>
3 <a> <b>1</b> <b>3</b> </a>
Another table:
hive> describe ranks;
id int
text string
hive> select * from ranks;
1 good
2 bad
3 worst
Create a view:
hive> create view xmlout(id, line) as select id, concat(f1,f2,f3,f4) from table1;
Then:
hive> select xpath_short(x.line, 'sum(a/b)'), r.text from xmlout x
join ranks r on (x.id = r.id);
2 good
3 bad
4 worst

How do I display a field's hidden characters in the result of a query in Oracle?

I have two rows that have a varchar column that are different according to a Java .equals(). I can't easily change or debug the Java code that's running against this particular database but I do have access to do queries directly against the database using SQLDeveloper. The fields look the same to me (they are street addresses with two lines separated by some new line or carriage feed/new line combo).
Is there a way to see all of the hidden characters as the result of a query?I'd like to avoid having to use the ascii() function with substr() on each of the rows to figure out which hidden character is different.
I'd also accept some query that shows me which character is the first difference between the two fields.
Try
select dump(column_name) from table
More information is in the documentation.
As for finding the position where the character differs, this might give you an idea:
create table tq84_compare (
id number,
col varchar2(20)
);
insert into tq84_compare values (1, 'hello world');
insert into tq84_compare values (2, 'hello' || chr(9) || 'world');
with c as (
select
(select col from tq84_compare where id = 1) col1,
(select col from tq84_compare where id = 2) col2
from
dual
),
l as (
select
level l from dual
start with 1=1
connect by level < (select length(c.col1) from c)
)
select
max(l.l) + 1position
from c,l
where substr(c.col1,1,l.l) = substr(c.col2,1,l.l);
SELECT DUMP('€ÁÑ', 1016)
FROM DUAL
... will print something like:
Typ=96 Len=3 CharacterSet=WE8MSWIN1252: 80,c1,d1

Resources