SUBSTR and INSTR function
Hi Gurus,
I have the data as follows:
data
'BIDIE01H/TXT:ZUNE=HA011, CellIndex=144 /CAI:452-01-32201-47001/CAI:45201F7dc9b79a'
'BIDIE01H/TXT:ZUNE=HA111, CellIndex=124 /CAI:452-01-32201-471/CAI:45201F7dc9b79b'
and I am trying to write a SQL to get the results:
CAI
452-01-32201-47001
452-01-32201-471
Any idea to get it done? I did try around with SUBSTR and INSTR functions but not yet sucessed.
Thanks,
Alex
select substr (str, instr (str, '/CAI:') + 5
, (instr (str, '/CAI:', -1) - instr (str, '/CAI:'))-5
from dataas in
SQL> with data
2 as
3 (select 'BIDIE01H/TXT:ZUNE=HA011, CellIndex=144 /CAI:452-01-32201-47001/CAI:45201F7dc9b79a' str from dual union all
4 select 'BIDIE01H/TXT:ZUNE=HA111, CellIndex=124 /CAI:452-01-32201-471/CAI:45201F7dc9b79b' from dual
5 )
6 select substr (str, instr (str, '/CAI:') + 5
7 , (instr (str, '/CAI:', -1) - instr (str, '/CAI:'))-5
8 )
9 from data
10 /
SUBSTR(STR,INSTR(STR,'/CAI:')+5,(INSTR(STR,'/CAI:',-1)-INSTR(STR,'/CAI:'))-5)
452-01-32201-47001
452-01-32201-471
Similar Messages
-
Convert varchar to date in BO Universe with Substring and instring function
to_date(substr(xxxx_v.e_details,instr(xxxx_v.e_details,'|',1,2)+1,9),'DD-MM-YYYY') In the universe while defining the object as date i was facing probs can any body help me out. the column xxxx_e_details is having the datatype varchar in database and the third string is date while i was creating the date object in the universe with the above syntax it was getting the error
The data in this obect is like this xxxx_v.e_details -> wwww|eeee|MM-DD-YY|
Edited by: mohammed kashif on Mar 31, 2011 11:47 AMi am getting the data but it is in the format 01-03-0201. The year format for 2010 is populating as 0201. i am not able to think whether it is due to instring function which is reading data like this. Need suggestion from experts.............
-
How to Split the string using Substr and instr using loop condition
Hi every body,
I have below requirement.
I need to split the string and append with single quotes('') followed by , (comma) and reassign entire values into another variable. so that i can use it where clause of update statement
for example I am reciveing value as follows
ALN varchar2(2000):=(12ERE-3MT-4Y,4IT-5O-SD,OP-K5-456,P04-SFS9-098,90P-SSF-334,3434-KJ4-O28,AS3-SFS0-J33,989-3KL-3434);
Note: In the above variable i see 8 transactions, where as in real scenario i donot how many transaction i may recive.
after modification i need above transactions should in below format
ALTR Varchar2(2000):=('12ERE-3MT-4Y','4IT-5O-SD','OP-K5-456','P04-SFS9-098','90P-SSF-334','3434-KJ4-O28','AS3-SFS0-J33','989-3KL-3434');
kindly help how to use substr and instr in normal loop or for loop or while loop while modifying the above transactions.
Please help me to sort out this issue.
Many Thanks.
Edited by: user627525 on Dec 15, 2011 11:49 AMTry this - may not be the best way but...:
create or replace type myTableType as table of varchar2(255)
declare
v_array mytabletype;
v_new_str varchar2(4000);
function str2tbl
(p_str in varchar2,
p_delim in varchar2 default '.')
return myTableType
as
l_str long default p_str || p_delim;
l_n number;
l_data myTableType := myTabletype();
begin
loop
l_n := instr( l_str, p_delim );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+length(p_delim) );
end loop;
return l_data;
end;
begin
v_array := str2tbl ('12ERE-3MT-4Y,4IT-5O-SD,OP-K5-456,P04-SFS9-098,90P-SSF-334,3434-KJ4-O28,AS3-SFS0-J33,989-3KL-3434', ',');
FOR i IN 1 .. v_array.COUNT LOOP
v_new_str := v_new_str || ''''||v_array(i)||'''' || ',';
END LOOP;
dbms_output.put_line(RTRIM(v_new_str, ','));
end;
OUTPUT:
=======
'12ERE-3MT-4Y','4IT-5O-SD','OP-K5-456','P04-SFS9-098','90P-SSF-334','3434-KJ4-O28','AS3-SFS0-J33','989-3KL-3434'HTH
Edited by: user130038 on Dec 15, 2011 12:11 PM -
Substri and instr problem --Please help
Hi ,
I would like to get the find tablename starts with 'EXP' using substring and instring . I am using oracle 9i. Please help me out
Example : 'Schema.explogtable'
I will use table name ' EXP' in one of my procedure as input parameter to procedure
If tablename= substr('schema.explogtable','instr('schema.explog','.',1,3) then
Execute Procedure1('tablename')
Else
Execute procedure2('tablename')
I would appreciate your help
Regards,
ClarkcClarkC,
Here just replace procedure1 and procedure2 with your procedure names;
create table temp_table
( table_name varchar2(30)
insert into temp_table values ('TESTME.MY_OBJECTS');
insert into temp_table values ('TESTME.OBJECTS');
insert into temp_table values ('ABC.ECFOBJECTLOG');
insert into temp_table values ('XYZ.BDEOBJECTTABLE');
insert into temp_table values ('ABC.ABCTABLE');
insert into temp_table values ('ZYD.CLIENTTABLE');
insert into temp_table values ('NMS.CLIENTLOGTABLE');
COMMIT;
pl/sql anonymous blocks I defined 2 variables for readibility and understanding
DECLARE
CURSOR tcur
IS
SELECT table_name
FROM temp_table;
table_name VARCHAR2 (40);
my_table VARCHAR2 (40);
BEGIN
FOR cur IN tcur
LOOP
my_table := NULL;
my_table := cur.table_name;
table_name := SUBSTR (my_table, INSTR (my_table, '.') + 1);
IF (table_name LIKE ('%OBJ%'))
THEN
DBMS_OUTPUT.put_line ('table_name containing OBJ=' || table_name);
-- NOTE : CALL YOUR PROCEDURE1 HERE for tables containing OBJ;
procedure1(table_name);
ELSE
DBMS_OUTPUT.put_line('table_name not containing OBJ=' || table_name);
-- NOTE : CALL YOUR PROCEDURE2 HERE for tables not containing OBJ;
procedure2(table_name);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SUBSTR (SQLERRM, 1, 300));
RAISE;
END;
Here is the output of the above block
table_name containing OBJ=MY_OBJECTS
table_name containing OBJ=OBJECTS
table_name containing OBJ=ECFOBJECTLOG
table_name containing OBJ=BDEOBJECTTABLE
table_name not containing OBJ=ABCTABLE
table_name not containing OBJ=CLIENTTABLE
table_name not containing OBJ=CLIENTLOGTABLEHope this helps
Regards
Edited by: OrionNet on Jan 17, 2009 11:48 AM -
Hello all,
I need help in simple query how can show only MCCODE
SELECT SUBSTR('VMTOPIC=MCCODE', INSTR('VMTOPIC=MCODE', 'VMTOPIC=')+8,20) AS output FROM DUAL;But also when i used
SELECT SUBSTR('VMTOPIC=MCCODE', INSTR('VMTOPIC=MCODE', 'HHH')+8,20) AS output FROM DUAL;show same results, what i need only if words begin with " VMTOPIC= " retrived characters after "VMTOPIC=".
regards
DheyaHi, Dheya,
Here's one way:
SELECT SUBSTR ( str
, INSTR ( str || 'VMTOPIC='
, 'VMTOPIC='
) + 8
, 20 -- or omit this argument
) AS output
FROM table_x
;where tabe_x.str is the string you need to test.
You could also do this with REGEXP_SUBSTR or REGEXP_REPLACE, but you can do this easily enough without any slow regular expressions.
973907 wrote:
... But also when i used
SELECT SUBSTR('VMTOPIC=MCCODE', INSTR('VMTOPIC=MCODE', 'HHH')+8,20) AS output FROM DUAL;show same results, what i need only if words begin with " VMTOPIC= " retrived characters after "VMTOPIC=".That's because 'HHH' wasn't found, and so INSTR returned 0. SUBSTR treats 0 like 1 in its 2nd argument, so, using that expresssion, not fnding the string is te same as finding it at position 1.
Of couse, looking for 'HHH' in a string that cotains 'VMTOPIC=' isn't the problem here; the real problem is looking for 'VMTOPIC=' in a string that doesn't contain it.
Making a special case when INSTR returns 0, as Solomon suggested, is one way to get around the problem. Another is what I posted above, which guarantees that INSTR will never return 0. If the 'VMTOPIC=' is not found in str, then lookng for it in str || 'VMTOPIC=' will cause INSTR to return a very high number; so high that SUBSTR will then return NULL.
I hope this answers your question.
If not, post CREATE TABLE and INSERT statements for a little sample data (maybe 5 rows), and the results you want from that data.
Point out where the query above is producing the wrong results, and explain, using specific examples, how you get those results from the sample data in those places.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
Substr and instr for variables assginement in a csv file
Hi gurus
Belows is my input csv file like with no fixedl enght
vinput_file:
WESTERN SAHARA,Moroccan Dirham,MAD,504,2,
YEMEN,Yemeni Rial,YER,886,2,
ZAMBIA,Zambian Kwacha,ZMW,967,2,
ZIMBABWE,Zimbabwe Dollar,ZWL,932,2,
and i want to assign 3 letter alphabetical to DESC and the 3 digit numerical to CODE
CODE := SUBSTR(vinput_rec,1,INSTR(vinput_file,',',1,1)-1);
DESC := SUBSTR(vinput_rec, INSTR(vinput_file,',',1,1)+1);
can anyone help me please with the above kindlyHi,
Sorry, it's not clear what you want.
975482 wrote:
Hi gurus
Belows is my input csv file like with no fixedl enghtIf you read the file as an external table, each comma-delimited substring will be a column. It should be easy to use SUBSTR on those columns
vinput_file:
WESTERN SAHARA,Moroccan Dirham,MAD,504,2,
YEMEN,Yemeni Rial,YER,886,2,
ZAMBIA,Zambian Kwacha,ZMW,967,2,
ZIMBABWE,Zimbabwe Dollar,ZWL,932,2,
and i want to assign 3 letter alphabetical to DESC and the 3 digit numerical to CODE
CODE := SUBSTR(vinput_rec,1,INSTR(vinput_file,',',1,1)-1);
DESC := SUBSTR(vinput_rec, INSTR(vinput_file,',',1,1)+1);
can anyone help me please with the above kindlyIf str is a comma-delimited string, and you want to the first 3 characters after the N-th comma, then you can use
SUBSTR ( str
, 1 + INSTR (str, ',', 1, n)
, 3
)Do you need to worry about not having 3 characters between the N-th comma and the next one? What if there are fewer than N commas?
I hope this answers your question.
If no, what is your question?
Does it involve reading a csv file that is not already in a table? Post a small sample file.
Does your qestion involve parsing strings that are already in a table? Post CREATE TABLE and INSERT statements.
In any case, post the exact results you want from the given data.
See the forum FAQ {message"id=9360002} -
Dear all,
I am very new to oracle. I am learning oracle now. I have small doubt could any one can help me.
I have a string like 'robert alias: 09-047 position now: CLARK'
now i need to store the word
'09-047' in to a variable x
and clark in to a variable y
i.e in X i have to store from the word 'robert alias:' till before the word 'position now:' then my out put is ' 09-047 '.
for this i tried as below
select trim(substr('robert alias: 09-047 position now: CLARK',
instr('robert alias: 09-047 position now: CLARK',':',1)+1)) from dual;
I am getting the o/p as '09-047 position now:: CLARK'
But i have to get '09-047' only. For that i need to give the 3rd parameter i.e length of the string.
In my substring i used only 2parameter i.e string and position now i have to give the length.
Could you please help me.
Thanks & Regards
Diliptest@ORA10G>
test@ORA10G> with t as (
2 select 'robert alias: 09-047 position now: CLARK' x from dual)
3 --
4 select x,
5 substr(x,instr(x,':',1,1)+2,instr(x,' position now')-instr(x,':',1,1)-2) y,
6 substr(x,instr(x,':',1,2)+2) z
7 from t;
X Y Z
robert alias: 09-047 position now: CLARK 09-047 CLARK
test@ORA10G>
test@ORA10G>isotope -
Another EQL question. Is there an equivalent to substr or instr in EQL?
Edited by: bewise on Jun 28, 2012 5:39 PMNot at this time. Check out the v2.3 EQL language reference guide here for more details.
http://docs.oracle.com/cd/E29805_01/QueryLangRef.pdf -
Substr and instr for long column
Plesae need support ,how can use instr and substr function for column with long datatype
select substr(rec,1,(instr(rec,'?',1))) from F_DE_O_HISTORY_QUEUE_PRE_TST2
rec column is long,When execute this select message displayed as ORA-00932: inconsistent datatypes: expected NUMBER got LONGTry to create a global temporary table and work on this table using DBMS_LOB.INSTR and DBMS_LOB.SUBSTR:
SQL> desc t;
Name Null? Type
X NUMBER(38)
Y LONG
SQL> create global temporary table tmp on commit preserve rows as select x, to_lob(y) as y from t ;
Table created.
SQL> desc tmp;
Name Null? Type
X NUMBER(38)
Y CLOBEdited by: P. Forstmann on 19 janv. 2010 12:42 -
How to make an substr and instr combination in javascrip
Dear all,
I need to extract some information in a javascript function.
In sql we can do like this substr(:P1_ITEM, 1, instr(:P1_ITEM,'.',1,4)-1)
f.e.
01.02.03.04.05 --> 01.02.03.04
001.002.003.004.005 --> 001.002.003.004
How I can do it in javascript.
Thanks in Advance
lukxHi lukx,
You can use the following javascript function:
function processText(pString, pSearchString, pOccurrenceNum)
var j = 0;
for(var i = 0; i < pOccurrenceNum; i++)
j = pString.indexOf(pSearchString,j) + 1;
j = j - 1;
return pString.substr(0,j);
}To process, for example, the second example you showed, you would have to do the following call:
processText("001.002.003.004.005", ".", 4)Maybe this is not the best solution but as I've seen in the javascript API that indexOf and lastIndexOf are only for the first and last occurrence, that's why I included the for loop. Hope this helps.
Regards,
Sergio -
Using multiple xdofx commands together. For eg: substr and instr
Hi,
I am working in developing an RTF where a situation is to use xdofx:sustr and xdofx:instr together.
I tried but its not working properly.
Is there any special commands to use in such a scenario.
Thanks,
Anandplz post what are you doing
works for me
as example
<?xdofx:substr(substr(COLOR,5),instr(substr(COLOR,5),’;’)+1)?>for
<ROWSET>
<ROW>
<COLOR>qwe qwe;rty</COLOR>
</ROW>
</ROWSET>gives
rty -
Using Substr and Instr together
Hi All,
I have a string like 'Employee_id,employee_name,employee_ssn,employee_ps'
I would like to extract all the four columns individually in a single statement.
For ex:
select SUBSTR('Employee_id,employee_name,employee_ssn,employee_ps',1,
INSTR('Employee_id,employee_name,employee_ssn,employee_ps',',',1,1)-1) from dual;
this gives me Employee_id. The same way how do i get the other coulmn names by comma position.
Pls help me out.
Please note that the column names have to be extracted depending on the comma position.
Thanks.You can use regular expressions if you're running an Oracle version that's 10g or more recent, to make it very simple:
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as FSITJA
SQL>
SQL> select regexp_substr('Employee_id,employee_name,employee_ssn,employee_ps', '[^,]+', 1, 1) col_1,
2 regexp_substr('Employee_id,employee_name,employee_ssn,employee_ps', '[^,]+', 1, 2) col_2,
3 regexp_substr('Employee_id,employee_name,employee_ssn,employee_ps', '[^,]+', 1, 3) col_3,
4 regexp_substr('Employee_id,employee_name,employee_ssn,employee_ps', '[^,]+', 1, 4) col_4
5 from dual;
COL_1 COL_2 COL_3 COL_4
Employee_id employee_name employee_ssn employee_ps
SQL> -
Have a look at this 'blk_employee_detail',
Now what i want to do is, remove the blk prefix and also the '_'.
and return employee detail.
dipinThank you a lot,
That works but i doubt it for forms.
Since you are trying to help me i think i shud write the whole scenario to you.
Here it goes.
I have got a number of blocks named starting with 'blk_..'.
and i need to display the block name on a display item using system.current_block.
But that would display me the real block name, the name i had kept for the block.
SO my problem is to format the block name,for example removing the 'blk_' and if there are some underscores in between the names then they too need to be removed.
Now i think the problem is crystal clear to you, buddy.
Thanx a lot.
DIpin -
Does substr and instr apply here?
Hi All,
I have this view or query which returns me month_between in about six decimal places, how do I cut it down to one decimal place.
see below
select empid,trunc(months_between(hire_date,dob)/12)Age_at_start, hire_date, (months_between(sysdate,hire_date)/12)Years_in_emp from employee;
EMPID AGE_AT_START HIRE_DATE YEARS_IN_EMP
1 38 10-JAN-05 1.26759241
2 32 12-JAN-05 1.26221606
3 30 21-JUN-00 5.82135585
4 39 12-JAN-04 2.26221606
5 25 29-DEC-05 .29985047
6 32 30-NOV-03 2.38049563
7 27 30-NOV-03 2.38049563
8 46 18-FEB-01 5.1627537
61 21 18-FEB-01 5.1627537
Can I do with some help please.
Much appreciation in advanced
cube60
Message was edited by:
cube60
Message was edited by:
cube60Perfect Jens,
SQL> select empid,trunc(months_between(hire_date,dob)/12)Age_at_start, hire_date, trunc((months_b
een(sysdate,hire_date)/12),1)Years_in_emp from employee;
EMPID AGE_AT_START HIRE_DATE YEARS_IN_EMP
1 38 10-JAN-05 1.2
2 32 12-JAN-05 1.2
3 30 21-JUN-00 5.8
4 39 12-JAN-04 2.2
5 25 29-DEC-05 .2
6 32 30-NOV-03 2.3
7 27 30-NOV-03 2.3
8 46 18-FEB-01 5.1
61 21 18-FEB-01 5.1
9 rows selected.
But can you explain the logic behind this, why the answer is 10
SELECT TRUNC(15.79,-1) "Truncate" FROM DUAL;
Truncate
10
many thanks
cube60 -
Dynamic PL/SQL & substr, instr function
I am having trouble with incorporating the SUBSTR and INSTR functions into my dynamic PL/SQL procedure using Oracle 8i.
I have data that is packed into one column seperated by a delimiter (':')
I need to seperate the data to use indicidual pieces.
If I run my query in general -
select substr(secondcol, 1, instr(secondcol, ':',1,1)-1) ONE,
substr(secondcol,instr(secondcol, ':',1,1)+1,instr(secondcol, ':',1,1)-1) TWO,
substr(secondcol,instr(secondcol, ':',1,2)+1,instr(secondcol, ':',1,1)-1) THREE,
substr(secondcol,instr(secondcol, ':',1,3)+1,instr(secondcol, ':',1,1)-1) FOUR
from temp_table where firstcol=100
This works and gives me the right result.
e.g
DATA :
Firstcol SECONDCOL
100 1:2:3:4
Result:
ONE TWO THREE FOUR
1 2 3 4
However to make this generic if I use it in a function passing it a parameter which has ':' delimited data it does not work and gives me errors. All I want is to get the output as a string that looks like my query above so I can use it in my proc.
create or replace function MYJUNK(TFieldNew IN CHAR)
RETURN CHAR IS
UpdateString Varchar2(100);
BEGIN
UpdateString := 'First=substr('||TFieldNew||', 1, instr('||TFieldNew||', '':'',1,1)-1) ONE, ';
UpdateString := UpdateString || ' Second=substr('||TFieldNew||', instr('||TFieldNew||', '':'',1,2)+1, instr('||TFieldNew||', '':'',1,1)-1) TWO, ';
UpdateString := UpdateString || ' third=substr('||TFieldNew||', instr('||TFieldNew||', '':'',1,3)+1, instr('||TFieldNew||', '':'',1,1)-1) THREE from temp_table';
return UpdateString;
END;
The function compiles but gives me run time errors
This is what I get -
SQL> select myjunk('''1:2:3:4''') from dual;
select myjunk('''1:2:3:4''') from dual
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SGHDTA.MYJUNK", line 8
ORA-06512: at line 1You are getting an error because updatestring is longer than the 100 characters you defined it as. Try using VARCHAR2(4000). Also, if you are trying to generate the sql statement, you need to get rid of first=, second= and third= when you build the string.
This is what your function returns. I put in line breaks for clarity:
First=substr('1:2:3:4', 1, instr('1:2:3:4', ':',1,1)-1) ONE,
Second=substr('1:2:3:4', instr('1:2:3:4', ':',1,2)+1, instr('1:2:3:4',':',1,1)-1) TWO,
third=substr('1:2:3:4', instr('1:2:3:4', ':',1,3)+1,instr('1:2:3:4', ':',1,1)-1) THREE
from temp_tableIf you are trying to actually parse the column, then you need something more like:
create or replace procedure MYJUNK(TFieldNew IN VARCHAR2,out1 OUT VARCHAR2,
out2 OUT VARCHAR2, out3 OUT VARCHAR2) is
BEGIN
out1 := SUBSTR(TFieldNew,1, INSTR(TFieldNew,':',1,1)-1);
out2 := SUBSTR(TFieldNew, INSTR(TFieldNew,':',1,2)+1, INSTR(TFieldNew,':',1,1)-1);
out3 := SUBSTR(, INSTR(TFieldNew,':',1,3)+1, INSTR(TFieldNew,':',1,1)-1);
END;
Maybe you are looking for
-
Trigger does not work after client upgrade to 8.1.7
I use a simple trigger to keep a table in three databases synchronized all worked fine with client 8.0.5 server 8.1.6. Now we are testing client 8.1.7 because another application needs it. The front end application is in vb (third party)accessing the
-
Can't get my 30" Apple LCD at full res with the MacBook Pro Retina
Hey I've been through a lot of posts and I can't seem to find a solution. I have a 30" Apple LCD monitor but the highest resolution I can get out of it through the MacBook Pro Retina is 1280 x 800. Any suggestions? I have tried holding down option wh
-
IMac overheating and unusually loud
For about a week, my iMac has been strangely overheating and loud. The only reason for this I can think of is that I recently moved my iMac to a different location, but still there is really no reason for this to be happening. My mac has adequate spa
-
Adding custom fields in IW41/42/43.
Hi All, I have a requirement of adding custom fields in the order confiramtion screen i.e IW41/42/43. i found this user exit CMFU0001: Determine customer-specific screen layout. but i am not able to figure out a way to use it. has anyone done such an
-
Windows Games compatability with OSX
I am curious if there is any way to operate these new computer cames coming out tagged Vista and XP compatible on my Macbook? Do i need to dual run windows with OSX or can I just install and go?