Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

split column value of a table and skip some words

Hil All,

I have a table , count is about 200M. It has a column which contains data separated by ‘~’. I want to parse it.

e.g:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Column1
A~B~C~D~E~F

Result :
Column_new1
A~C~E

I just want to skip 2,4,6,n th. words. I don’t want plsql. I need sql query. And table is very big,I also need performance.

I use substr,instr functions and I can parse. But it runs really slowly..

Thanks for help.

>Solution :

If you are after performance then use the INSTR and SUBSTR simple string functions:

SELECT SUBSTR(column1, 1,      p1 - 1 ) || '~' ||
       SUBSTR(column1, p2 + 1, p3 - p2 - 1) || '~' ||
       SUBSTR(column1, p4 + 1, p5 - p4 - 1) AS column1_new
FROM   (
  SELECT column1,
         INSTR(column1, '~', 1, 1) AS p1,
         INSTR(column1, '~', 1, 2) AS p2,
         INSTR(column1, '~', 1, 3) AS p3,
         INSTR(column1, '~', 1, 4) AS p4,
         INSTR(column1, '~', 1, 5) AS p5
  FROM   table_name
);

Which, for the sample data:

CREATE TABLE table_name (column1) AS
SELECT 'A~B~C~D~E~F' FROM DUAL;

Outputs:

COLUMN1_NEW
A~C~E

If you want a shorter query then you can use regular expressions:

SELECT REGEXP_REPLACE(column1, '([^~]+)~[^~]+~([^~]+)~[^~]+~([^~]+).*', '\1~\2~\3' )
         AS column1_new
FROM   table_name;

However, you will find that performance is likely to be an order of magnitude worse than simple string functions.


Another alternative would be to generate a materialized view.

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading