This blog was created to improve my English and share news, thoughts and opinions about the world around me. I use it also to fix some technical solutions for future use.
venerdì 22 marzo 2013
Oracle Function to get a specific field from a String contains fields characters delimited
Hi All
Today a simple function to extract a field from a String contains characters delimited fields.
I want use regexp_substr function but there are some problems for null fields to extract, so i develop this simple function.
CREATE OR REPLACE FUNCTION
get_field (iString IN VARCHAR2, iSeparator IN VARCHAR2, field_num IN NUMBER)
RETURN VARCHAR2
IS
oField VARCHAR2(1024);
position_field NUMBER;
position_field_start NUMBER;
position_field_end NUMBER;
BEGIN
IF field_num = 1 THEN
position_field := INSTR(iString,iSeparator);
IF position_field > 0 THEN
SELECT SUBSTR(iString,1,INSTR(iString,iSeparator)-1) INTO oField FROM DUAL;
ELSE
oField := iString;
END IF;
ELSE
position_field_start := INSTR(iString,iSeparator,1,field_num-1);
position_field_end := INSTR(iString,iSeparator,1,field_num);
IF position_field_start > 0 THEN
IF position_field_end = 0 THEN
position_field_end := length(iString)+1;
END IF;
SELECT SUBSTR(iString,position_field_start+1,position_field_end-position_field_start-1) INTO oField FROM DUAL;
ELSE
oField := null;
END IF;
END IF;
RETURN (oField);
END;
For example you have this string '10;12;34;233;12' delimited by ';' character and you wont get third field.
You can call this oracle function in this way :
SELECT GET_FIELD('10;12;34;233;12',';',3) FROM DUAL;
Results will be : '34'.
I hope can help someone with same problem.
Stay tuned
Iscriviti a:
Commenti sul post (Atom)
Check Mid Year Objectives
Hi all Today middle year check of my 2026's goals. 1. ENGLISH Improve listening comprehension (So and So) See at least one or two film...
-
Hi all Today I want speak about one of the best DB Client tool in the market : DBeaver . Why it's so good : 1) It's an open source...
-
Hi all Today, first day of 2025 my goals for this year : 1. ENGLISH Improve listening comprehension See at least one or two films in Engli...
-
For my actual project i am studying CUBE and ROLLUP option in GROUP BY. These options are present from Oracle 8i and are very interesting : ...
Nessun commento:
Posta un commento