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)
Goals 2025
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...
-
Hi all Today i want speak about education and eLearning websites. Personally I am using COURSERA , in this site you can find many course...
-
Hi all Today i want to speak about oracle nested table. Basically you can define in oracle a field that is a table inserted in other tabl...
-
Today a simple shell script to simulate rolls of two dices : #!/bin/ksh fc=1 while [ fc -eq 1 ]; do clear echo ' How many rolls : \c...
Nessun commento:
Posta un commento