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>

Similar Messages

  • 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 AM

    Try 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,
    Clarkc

    ClarkC,
    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

  • 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

  • Hibernate problem..... using projection and conjunction together..

    hi all ,
    can we use projection and conjunction together??
    i want to execute a query like this..
    select column_1 , column_2 from table_name where column_1 like '%a' ;

    hi ian
    you could also try verifying if the jar libraries containing your Document and Paragraph classes are defined in your class path OR if you are using an IDE, did you include that under your LIbraries?
    The previous suggestion below also is a stop-gap to the library inclusion problem.
    Hope that helps

  • Use bottom and protect together

    Hi all,
    May I know the that possible to use BOTTOM and PROTECT together with few lines of text inbetween?
    Eg:
    BOTTOM
    PROTECT
    texttexttext
    texttexttext
    texttexttext
    ENDPROTECT
    ENDBOTTOM
    or
    PROTECT
    BOTTOM
    texttexttext
    texttexttext
    texttexttext
    ENDBOTTOM
    ENDPROTECT
    Above code dont work for me. Please advice thank!!!

    Hi Kek,
    Refer this link,
    [Bottom..Endbottom|http://help.sap.com/saphelp_nw70/helpdata/en/d6/0db4a9494511d182b70000e829fbfe/content.htm]
    Regards,
    Sravanthi

  • Using Concat and Distinct together

    hi
    I am using the query below and I am getting 'missing expression' error. How do use Distinct and concat together?
    SELECT 'SELECT ' || DISTINCT COLUMN || ' FROM DUAL UNION ALL '
    FROM TABLE;

    Hi,
    SeshuGiri wrote:
    hi
    I am using the query below and I am getting 'missing expression' error. How do use Distinct and concat together?
    SELECT 'SELECT ' || DISTINCT COLUMN || ' FROM DUAL UNION ALL '
    FROM TABLE;Whenever you have a problem, please post CREATE TABLE and INSERT statments for a little sample data, and the results you want from that data.
    The example you posted has so many mistakes, it's hard to know what you're trying to do.
    COLUMN, DISTINCT and TABLE are all Oracle keywords, and shouldn't be used for actual columnor table names, and using them for aliases is just asking for trouble.
    When using SELECT DISTINCT, the keyword DISTINCT must come immediately after SELECT.
    If you want to assign an alias to a column, it must be at the very end of the expression that defines the column. You can't assign an alias to part of an expression. If you want to do that, use a sub-query.
    Here's a query that uses both SELECT DISTINCT and ||:
    SELECT DISTINCT
            job || deptno    AS jd
    FROM    scott.emp;Edited by: Frank Kulash on May 13, 2011 2:52 PM

  • Using Airport And Ethernet Together

    Having read Christopher Breen's solution to using Airport and Ethernet together,
    http://www.macworld.com/weblogs/mac911/2006/10/airportswitch/index.php
    I am still wondering if this can apply to my situation. I have an Airport Exteme BS distributing the wireless signal on it's own node. Nothing is physically connected to the BS except an ethernet cable running to a hub which is connected to the cable modem. While my ISP service isn't supplying service faster than Airport will manage, no problem...but, I would like to have the advantage ethernet networking between my machines for all the advantages that ethernet speed can bring to that situation.
    My connection to the internet is from WAN from the cable modem. I have an ethernet connection from the BS that I can run a cable back to say, an iBook. Will this work? I want wireless internet, but ethernet for machine to machine networking.
    JAL
    iBook G3/500 G4/Dual 450 Mac OS X (10.4)
    iBook G3/500 G4/Dual 450 Mac OS X (10.4)

    Helpful response, but I am uncertain this is what I am after. First, the hub is at the other end of the house, inconvenient to run a cable to it in this way. Presently, "Distribute IP Addresses" is turned off. This is way it had to be set since the AEBS is on a separate node. I just wonder why I can't run a cable from the AEBS LAN port back to the iBook. Looking for best possible speed when networking from my studio G4 while keeping the wireless internet setting.
    Any reason making the config the way I am imagining will not work?

  • I want to use ps and illustrator together, but I just bought the photoshop membership, could I upgrade my membership?

    I want to use ps and illustrator together, but I just bought the photoshop membership, could I upgrade my membership?

    You may upgrade to the entire plan, or just add an individual program subscription
    Upgrade single to all Cloud http://forums.adobe.com/thread/1235382
    Cloud Plans https://creative.adobe.com/plans
    -and subscription terms http://www.adobe.com/misc/subscription_terms.html
    -what is in the entire Cloud http://www.adobe.com/creativecloud/catalog/desktop.html
    -http://www.adobe.com/products/catalog/mobile._sl_id-contentfilter_sl_catalog_sl_mobiledevi ces.html

  • Using SUBSTRING and CHARINDEX

    I have these values in a column:
    \mssql$scdb:access methods
    \mssql$scdb:buffer manager
    \mssql$scdb:general statistics
    \mssql$scdb:locks(_total)
    \mssql$scdb:memory manager
    \mssql$scdb:sql statistics
    \sqlserver:access methods
    \sqlserver:buffer manager
    \sqlserver:general statistics
    \sqlserver:locks(_total)
    \sqlserver:memory manager
    \sqlserver:sql statistics
    I want to write code to seperate into two columns, eliminating the \ and the :.
    mssql$scdb                                        access methods
    mssql$scdb                                        buffer manager
    mssql$scdb                                        general statistics
    mssql$scdb                                        locks(_total)
    mssql$scdb                                        memory manager
    mssql$scdb                                        sql statistics
    sqlserver                                           
    access methods
    sqlserver                                           
    buffer manager
    sqlserver                                            general
    statistics
    sqlserver                                            locks(_total)
    sqlserver                                           
    memory manager
    sqlserver                                            sql
    statistics
    I have tried to use substring and charindex and I cannot seem to get it to work.
    Could someone please help?
    lcerni

    Hi,
    Try this.
    DECLARE @Tbl TABLE(Value VARCHAR(150))
    INSERT INTO @Tbl(Value)
    VALUES('\mssql$scdb:access methods'),
    ('\mssql$scdb:buffer manager'),
    ('\mssql$scdb:general statistics'),
    ('\mssql$scdb:locks(_total)'),
    ('\mssql$scdb:memory manager'),
    ('\mssql$scdb:sql statistics'),
    ('\sqlserver:access methods'),
    ('\sqlserver:buffer manager'),
    ('\sqlserver:general statistics'),
    ('\sqlserver:locks(_total)'),
    ('\sqlserver:memory manager'),
    ('\sqlserver:sql statistics')
    SELECT REPLACE(SUBSTRING(Value, 0,CHARINDEX(':',Value)),'\','') Column1,
    SUBSTRING(Value, CHARINDEX(':',Value)+1, LEN(Value)) Column2
    FROM @Tbl
    - Pls mark as answer/vote, if this post is helpful.
    Sugumar Pannerselvam

  • 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,
    Anand

    plz 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

  • SUBSTR and INSTR query

    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
    Dheya

    Hi, 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 kindly

    Hi,
    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}

  • Substring and instring

    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
    Dilip

    test@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

  • Using generics and J2SE together with JSF

    Hi
    Has anybody experience with using JSF together with J2SE 1.5? Especially generics? I would like to use generics and other improvements from J2SE in the model objects.
    Thnaks in advance ... Rick

    JSF 1.1 is specified for J2SE 5, and the new specification (JSF 1.2 and JSP 2.1) does not include generics.
    If you are talking in terms of ValueBindings or MethodBindings, there really isn't any point since the implementation could only make the cast for you; and it would still not be able to garuantee type safety (the only reason to use generics).
    That's not to say you couldn't use generics in your own development, but I don't think you will see generics incorporated into the specification any time soon.
    Jacob Hookom (JSR-252 EG)

Maybe you are looking for

  • How to chnage the row text in report

    Hi We have a  tailored requirement. Kindly help me out. In our report , the rows are defined and it has characteristic Account number ( GL ) . In our report GL  we want to change the description of rows ( GL account number ) for non account user. So

  • Database auto restart at 2:00 AM every day

    Hi, I've installed 10.2.0.2.0 database in Windows 2000 Server. Now it restarts automatically at 2:00 AM every day. I've checked OS, there is no JOB run at 2:00 AM, and event viewer look normal at 2:00 AM. When it restarts, sometimes it would restart

  • I did iPad update and now my Adobe pdf files will not open!

    My update on iPad now wont open Adobe pdf files. Help...

  • VISTA POR CUENTA ASOCIADA

    La cuestión es que nos acaban de actualizar la versión de SAP. ANTES, cuando entraba al saldo del Interlocutor Comercial, directamente veía el saldo distribuido en cuentas asociadas. AHORA, al darle a visualizar saldo, sale directamente el libro mayo

  • Is there a trial version to see how it works?

    Is there a trial version to see how it works?