REGEXP_SUBSTR() help

Hi:
I've got a bunch of triggers that I'm trying to get rid of. They are BEFORE UPDATE OF x1,x2,x3... triggers and they basically prevent the user from updating the columns. I want to do away with these and make the table declarations handle it (grant select only on the columns not found in the trigger of a given table). So step 1 is I'm trying to go through the user_triggers description column and parse out the columns that the current trigger applies to. I've simplified the example by including text directly into the REGEXP call but I'm really using it in a larger query on user_triggers and the REGEXP is working off of the description column contents.
My problem is that I want a way to extract the columns and only the columns. I currently have:
select upper(REGEXP_SUBSTR(
'TRIGGER "GAFF".access_tbus
   BEFORE UPDATE OF access_code,foo1_col,foo2_col ON access_tbl
BEGIN
   s2_error_pck.update_not_allowed(''Access_TBL column Access_Code, foo1_col, foo2_col'');
END;',
       '(((before update)[ ]{1})'                  -- keywords followed by a space
      || '(\s*[[:alnum:]_]+\s*,){0,10})'        -- 0 to 10 instances of column_name followed by comma
      || '(\s*([[:alnum:]_]\s*).*)',1,1,'i'))     -- the final (perhaps only) column name, not followed by comma
from dualbut the result is
BEFORE UPDATE OF ACCESS_CODE,FOO1_COL,FOO2_COL ON ACCESS_TBLI currently have this working (I think!) with 1-n columns in the list but I get more than I want in the output. How do I user the BEFORE UPDATE OF to match but not include it or the table name in the result? (I'd ideally like to tack on a ' ON ' in the match as well if I can do that but not get stuck with that in the output as well).
Thanks,
Gaff

Hi,
Example:
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
SQL>
SQL> SELECT ut.trigger_name,
  2         ut.trigger_type,
  3         ut.triggering_event,
  4         utc.table_name,
  5         utc.column_name
  6    FROM user_triggers     ut,
  7         user_trigger_cols utc
  8   WHERE ut.trigger_name = utc.trigger_name
  9   ORDER BY ut.trigger_name,
10            utc.table_name,
11            utc.column_name;
TRIGGER_NAME                   TRIGGER_TYPE     TRIGGERING_EVENT                                                                 TABLE_NAME                     COLUMN_NAME
UPDATE_JOB_HISTORY             AFTER EACH ROW   UPDATE                                                                           EMPLOYEES                      DEPARTMENT_ID
UPDATE_JOB_HISTORY             AFTER EACH ROW   UPDATE                                                                           EMPLOYEES                      EMPLOYEE_ID
UPDATE_JOB_HISTORY             AFTER EACH ROW   UPDATE                                                                           EMPLOYEES                      HIRE_DATE
UPDATE_JOB_HISTORY             AFTER EACH ROW   UPDATE                                                                           EMPLOYEES                      JOB_ID
SQL> Regards,
Edited by: Walter Fernández on Feb 23, 2009 5:13 PM

Similar Messages

  • [Mostly Sorted] Extracting tags - regexp_substr and count help needed!

    My original query got sorted, but additional regexp_substr and count help is required further on down!
    Hi,
    I have a table on a 10.2.0.3 database which contains a clob field (sql_stmt), with contents that look something like:
    SELECT <COB_DATE>, col2, .... coln
    FROM   tab1, tab2, ...., tabn
    WHERE tab1.run_id = <RUNID>
    AND    tab2.other_col = '<OTHER TAG>'(That's a highly simplified sql_stmt example, of course - if they were all that small we'd not be needing a clob field!).
    I wanted to extract all the tags from the sql_stmt field for a given row, so I can get my (well not "mine" - I'd never have designed something like this, but hey, it works, sorta, and I'm improving it as and where I can!) pl/sql to replace the tags with the correct values. A tag is anything that's in triangular brackets (eg. <RUNID> from the above example)
    So, I did this:
    SELECT     SUBSTR (sql_stmt,
                       INSTR (sql_stmt, '<', 1, LEVEL),
                       INSTR (substr(sql_stmt, INSTR (sql_stmt, '<', 1, LEVEL)), '>', 1, 1)
                       ) tag
    FROM       export_jobs
    WHERE      exp_id =  p_exp_id
    CONNECT BY LEVEL <= (LENGTH (sql_stmt) - LENGTH (REPLACE (sql_stmt, '<')))Which I thought would be fine (having tested it on a text column). However, it runs very poorly against a clob column, for some reason (probably doesn't like the substr, instr, etc on the clob, at a guess) - the waits show "direct path read".
    When I cast the sql_stmt as a varchar2 like so:
    with my_tab as (select cast(substr(sql_stmt, instr(sql_stmt, '<', 1), instr(sql_stmt, '>', -1) - instr(sql_stmt, '<', 1) + 1) as varchar2(4000)) sql_stmt
                    from export_jobs
                    WHERE      exp_id = p_exp_id)
    SELECT     SUBSTR (sql_stmt,
                       INSTR (sql_stmt, '<', 1, LEVEL),
                       INSTR (substr(sql_stmt, INSTR (sql_stmt, '<', 1, LEVEL)), '>', 1, 1)
                       ) tag
    FROM       my_tab
    CONNECT BY LEVEL <= (LENGTH (sql_stmt) - LENGTH (REPLACE (sql_stmt, '<')))it runs blisteringly fast in comparison, except when the substr'd sql_stmt is over 4000 chars, of course! Using dbms_lob instr and substr etc doesn't help either.
    So, I thought maybe I could find an xml related method, and from this link:get xml node name in loop , I tried:
    select t.column_value.getrootelement() node
      from (select sql_stmt xml from export_jobs where exp_id = 28) xml,
    table (xmlsequence(xml.xml.extract('//*'))) tBut I get this error: ORA-22806: not an object or REF. (It might not be the way to go after all, as it's not proper xml, being as there are no corresponding close tags, but I was trying to think outside the box. I've not needed to use xml stuff before, so I'm a bit clueless about it, really!)
    I tried casting sql_stmt into an xmltype, but I got: ORA-22907: invalid CAST to a type that is not a nested table or VARRAY
    Is anyone able to suggest a better method of trying to extract my tags from the clob column, please?
    Message was edited by:
    Boneist

    I don't know if it may work for you, but I had a similar activity where I defined sql statements with bind variables (:var_name) and then I simply looked for witch variables to bind in that statement through this query.
    with x as (
         select ':var1
         /*a block comment
         :varname_dontcatch
         select hello, --line comment :var_no
              ''a string with double quote '''' and a :variable '',  --:variable
              :var3,
              :var2, '':var1'''':varno'',
         from dual'     as string
         from dual
    ), fil as (
         select string,
              regexp_replace(string,'(/\*[^*]*\*/)'||'|'||'(--.*)'||'|'||'(''([^'']|(''''))*'')',null) as res
         from x
    select string,res,
         regexp_substr(res,'\:[[:alpha:]]([[:alnum:]]|_)*',1,level)
    from fil
    connect by regexp_instr(res,'\:[[:alpha:]]([[:alnum:]]|_)*',1,level) > 0
    /Or through these procedures
         function get_binds(
              inp_string in varchar2
         ) return string_table
         deterministic
         is
              loc_str varchar2(32767);
              loc_idx number;
              out_tab string_table;
         begin
              --dbms_output.put_line('cond = '||inp_string);
              loc_str := regexp_replace(inp_string,'(/\*[^*]*\*/)'||'|'||'(--.*)'||'|'||'(''([^'']|(''''))*'')',null);
              loc_idx := 0;
              out_tab := string_table();
              --dbms_output.put_line('fcond ='||loc_str);
              loop
                   loc_idx := regexp_instr(loc_str,'\:[[:alpha:]]([[:alnum:]]|_)*',loc_idx+1);
                   exit when loc_idx = 0;
                   out_tab.extend;
                   out_tab(out_tab.last) := regexp_substr(loc_str,'[[:alpha:]]([[:alnum:]]|_)*',loc_idx+1);
              end loop;
              return out_tab;
         end;
         function divide_string (
              inp_string in varchar2
              --,inp_length in number
         --return string_table
         return dbms_sql.varchar2a
         is
              inp_length number := 256;
              loc_ind_1 pls_integer;
              loc_ind_2 pls_integer;
              loc_string_length pls_integer;
              loc_curr_string varchar2(32767);
              --out_tab string_table;
              out_tab dbms_sql.varchar2a;
         begin
              --out_tab := dbms_sql.varchar2a();
              loc_ind_1 := 1;
              loc_ind_2 := 1;
              loc_string_length := length(inp_string);
              while ( loc_ind_2 < loc_string_length ) loop
                   --out_tab.extend;
                   loc_curr_string := substr(inp_string,loc_ind_2,inp_length);
                   dbms_output.put(loc_curr_string);
                   out_tab(loc_ind_1) := loc_curr_string;
                   loc_ind_1 := loc_ind_1 + 1;
                   loc_ind_2 := loc_ind_2 + length(loc_curr_string);
              end loop;
              dbms_output.put_line('');
              return out_tab;
         end;
         function execute_statement(
              inp_statement in varchar2,
              inp_binds in string_table,
              inp_parameters in parametri
         return number
         is
              loc_stat dbms_sql.varchar2a;
              loc_dyn_cur number;
              out_rows number;
         begin
              loc_stat := divide_string(inp_statement);
              loc_dyn_cur := dbms_sql.open_cursor;
              dbms_sql.parse(c => loc_dyn_cur,
                   statement => loc_stat,
                   lb => loc_stat.first,
                   ub => loc_stat.last,
                   lfflg => false,
                   language_flag => dbms_sql.native
              for i in inp_binds.first .. inp_binds.last loop
                   DBMS_SQL.BIND_VARIABLE(loc_dyn_cur, inp_binds(i), inp_parameters(inp_binds(i)));
                   dbms_output.put_line(':'||inp_binds(i)||'='||inp_parameters(inp_binds(i)));
              end loop;
              dbms_output.put_line('');
              --out_rows := DBMS_SQL.EXECUTE(loc_dyn_cur);
              DBMS_SQL.CLOSE_CURSOR(loc_dyn_cur);
              return out_rows;
         end;Bye Alessandro
    Message was edited by:
    Alessandro Rossi
    There is something missing in the functions but if there is something that may interest you you can ask.

  • Help me about built-in functions like regexp_substr,regexp_replace

    Hi everybody
    Can anyone help me to understand these functions like regexp_substr,regexp_replace ...
    Will be better if documantation include examples with different situations or it may be links
    Thx

    also - if you're just trying to learn regular expressions - which are generic and very much not specific to oracle, there are plenty of tutorial websites around the place that you'll find by googling.
    well worth learning, regardless of the programming language you're working with.

  • Help with regexp_substr

    i can have string like this
    '<i>Terry Lewis<\i><i>Drape Neck Tank<\i><i>or Bias Skirt<\i><i><\i><i><\i><i><\i><i><\i><i><\i><i>$49.00-59.00<\i><i>$2.50<\i><i>1<\i><i>$2.50<\i><i><\i><i><\i><i><\i><i>Cocoa, Turquoise or Poppy<\i><i>Missy: 4,6,8,10,12,14,16<\i><i>Women: 18W,20W,22W,24W<\i><i><\i><i><\i><i><\i><i><\i><i>N<\i><i><\i><i><\i><i>Clearance Price<\i><i>Y/C: TL Drape Neck Tank or Skirt<\i><i><\i><i>$5.20-$5.20</i><i></i><i></i>'
    or <i></i><i>$1.00</i><i></i><$3.00</i>
    I want to capture the null value but i couldn't using the below
    query any help would be appreciated
    select regexp_substr('<i>Terry Lewis<\i><i>Drape Neck Tank<\i><i>or Bias Skirt<\i><i><\i><i><\i><i><\i><i><\i><i><\i><i>$49.00-59.00<\i><i>$2.50<\i><i>1<\i><i>$2.50<\i><i><\i><i><\i><i><\i><i>Cocoa, Turquoise or Poppy<\i><i>Missy: 4,6,8,10,12,14,16<\i><i>Women: 18W,20W,22W,24W<\i><i><\i><i><\i><i><\i><i><\i><i>N<\i><i><\i><i><\i><i>Clearance Price<\i><i>Y/C: TL Drape Neck Tank or Skirt<\i><i><\i><i>$5.20-$5.20</i><i></i><i></i>' col1,'[^<i>]+', 1, level) list
    from dual connect by level <= NVL( LENGTH( REGEXP_REPLACE( '<i>Terry Lewis<\i><i>Drape Neck Tank<\i><i>or Bias Skirt<\i><i><\i><i><\i><i><\i><i><\i><i><\i><i>$49.00-59.00<\i><i>$2.50<\i><i>1<\i><i>$2.50<\i><i><\i><i><\i><i><\i><i>Cocoa, Turquoise or Poppy<\i><i>Missy: 4,6,8,10,12,14,16<\i><i>Women: 18W,20W,22W,24W<\i><i><\i><i><\i><i><\i><i><\i><i>N<\i><i><\i><i><\i><i>Clearance Price<\i><i>Y/C: TL Drape Neck Tank or Skirt<\i><i><\i><i>$5.20-$5.20</i><i></i><i></i>' col1, '[^<i>]+', NULL ) ), 0 ) + 1;
    thanks

    i can have string like this
    Terry Lewis&lt;\i&gt;Drape Neck Tank&lt;\i&gt;or Bias Skirt&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;$49.00-59.00&lt;\i&gt;$2.50&lt;\i&gt;1&lt;\i&gt;$2.50&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;Cocoa, Turquoise or Poppy&lt;\i&gt;Missy: 4,6,8,10,12,14,16&lt;\i&gt;Women: 18W,20W,22W,24W&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;N&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;Clearance Price&lt;\i&gt;Y/C: TL Drape Neck Tank or Skirt&lt;\i&gt;&lt;i&gt;$5.20-$5.20&lt;/i&gt;
    or can be like this
    &lt;i&gt;&lt;/i&gt; &lt;i&gt;$3.00&lt;/i&gt;&lt;i&gt;&lt;/i&gt;
    I want to capture the null value but i couldn't using the below
    query any help would be appreciated
    select regexp_substr('Terry Lewis&lt;\i&gt;Drape Neck Tank&lt;\i&gt;or Bias Skirt&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;$49.00-59.00&lt;\i&gt;$2.50&lt;\i&gt;1&lt;\i&gt;$2.50&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;Cocoa, Turquoise or Poppy&lt;\i&gt;Missy: 4,6,8,10,12,14,16&lt;\i&gt;Women: 18W,20W,22W,24W&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;N&lt;\i&gt;&lt;\i&gt;&lt;\i&gt;Clearance Price&lt;\i&gt;Y/C: TL Drape Neck Tank or Skirt&lt;\i&gt;&lt;\i&gt;$5.20-$5.20' col1,'^+', 1, level) list
    from dual connect by level &lt;= NVL( LENGTH( REGEXP_REPLACE( '&lt;i&gt;Terry Lewis&lt;\i&gt;&lt;i&gt;Drape Neck Tank&lt;\i&gt;&lt;i&gt;or Bias Skirt&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;$49.00-59.00&lt;\i&gt;&lt;i&gt;$2.50&lt;\i&gt;&lt;i&gt;1&lt;\i&gt;&lt;i&gt;$2.50&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;Cocoa, Turquoise or Poppy&lt;\i&gt;&lt;i&gt;Missy: 4,6,8,10,12,14,16&lt;\i&gt;&lt;i&gt;Women: 18W,20W,22W,24W&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;N&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;Clearance Price&lt;\i&gt;&lt;i&gt;Y/C: TL Drape Neck Tank or Skirt&lt;\i&gt;&lt;i&gt;&lt;\i&gt;&lt;i&gt;$5.20-$5.20&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;' col1, '^&lt;i&gt;+', NULL ) ), 0 ) + 1;

  • Query help in regular expression Query

    Hi all,
    Version details
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE     11.1.0.7.0     Production
    TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - ProductionI have table RULE with one column as CLOB data type and my requirement is as follows
    Sample Data :
    0-7IfFlowControl0.-7dd670afb-2d41-440f-958d-c19f0f75e91dareErrorsPostedAtHeader(00760)ClaimErrorCollection0.-7-1000FlowControl0-8ThenFlowControl0.-8a0c1c903-0c04-4d68-b5a8-10cbd4a03e9faddRequiredElementErrors(00760 & D)
    Expected Output .
    00760
    00760
    D
    I want the values within braces() and when ever there is an & symbol inside braces then i want it in the next row.
    Please see the above example .Any help in this regard is would be highly appreciated ...........
    Thanks,
    P Prakash
    Edited by: prakash on Nov 10, 2011 9:19 PM

    with t
    as
    select '0-7IfFlowControl0.-7dd670afb-2d41-440f-958d-c19f0f75e91dareErrorsPostedAtHeader(00760)ClaimErrorCollection0.-7-1000FlowControl0-8ThenFlowControl0.-8a0c1c903-0c04-4d68-b5a8-10cbd4a03e9faddRequiredElementErrors(00760 & D)' str
      from dual
    ), t1
    as
    select replace(replace(regexp_substr(str, '\([^)]*\)', 1, level),')'), '(') str
      from t
    connect by level <= regexp_count(str, '\([^)]*\)')
    select regexp_substr(str, '[^&]+', 1, t2.l)
      from t1
    cross join
             select level l
               from (
                      select max(length(regexp_replace(str, '[^&]')))+1 cnt
                        from t1
            connect by level <= cnt
           ) t2
    where regexp_substr(str, '[^&]+', 1, t2.l) is not null

  • SQL Query help ( On connect By level clause)

    Hi all,
    I have this query developed with data in with clause.
    With dat As
      select '@AAA @SSS @DDD' col1 from dual union all
      select '@ZZZ @XXX @TTT @RRR @ZZA' col1 from dual
    Select regexp_substr( col1 , '[^@][A-Z]+',1,level) Show from dat
    connect by level  <= regexp_count(col1, '@');Current output :-
    SHOW
    AAA
    SSS
    DDD
    RRR
    ZZA
    TTT
    RRR
    ZZA
    XXX
    DDD
    RRR
    SHOW
    ZZA
    TTT
    RRR
    ZZA
    . . .1st row comes fine, But next row data is getting duplicated. And total record count = 30. I tried with some but didn't work.
    Expected output :-
    SHOW
    AAA
    SSS
    DDD
    ZZZ
    XXX
    TTT
    RRR
    ZZAI need some change on my query and I am not able to find that. So anybody can add on that or can also provide some different solution too.
    Thanks!
    Ashutosh

    Hi,
    When you use something like "CONNECT BY LEVEL <= x", then at least one of the following must be true:
    (a) the table has no more than 1 row
    (b) there are other conditions in the CONNECT BY clause, or
    (c) you know what you are doing.
    To help see why, run this query
    SELECT     SYS_CONNECT_BY_PATH (dname, '/')     AS path
    ,     LEVEL
    FROM     scott.dept
    CONNECT BY     LEVEL <= 3
    ;and study the results:
    PATH                                     LEVEL
    /ACCOUNTING                                  1
    /ACCOUNTING/ACCOUNTING                       2
    /ACCOUNTING/ACCOUNTING/ACCOUNTING            3
    /ACCOUNTING/ACCOUNTING/RESEARCH              3
    /ACCOUNTING/ACCOUNTING/SALES                 3
    /ACCOUNTING/ACCOUNTING/OPERATIONS            3
    /ACCOUNTING/RESEARCH                         2
    /ACCOUNTING/RESEARCH/ACCOUNTING              3
    /ACCOUNTING/RESEARCH/RESEARCH                3
    /ACCOUNTING/RESEARCH/SALES                   3
    /ACCOUNTING/RESEARCH/OPERATIONS              3
    /ACCOUNTING/SALES                            2
    /ACCOUNTING/SALES/ACCOUNTING                 3
    84 rows selected.

  • Help in query using regular expression

    HI,
    I need a help to get the below output using regular expression query. Please help me.
    SELECT REGEXP_SUBSTR ('PWRPKG(P/W+P/L+CC)', '[^+]+', 1, lvl) val, lvl
    FROM DUAL,(SELECT LEVEL lvl FROM DUAL
    CONNECT BY LEVEL <=(SELECT MAX ( LENGTH ('PWRPKG(P/W+P/L+CC)') - LENGTH (REPLACE ('PWRPKG(P/W+P/L+CC)','+',NULL))+ 1) FROM DUAL));
    I need the output as
    correct result:
    ==============
    val lvl
    P/W 1
    P/L 2
    CC 3
    But i tried the above it is not coming the above result. Please help me where i did a mistake.
    Thanks in advance

    Frank gave you a solution in your other thread. You could simplify it if you are on 11g:
    SQL> select * from table_x
      2  /
    TXT
    TECHPKG(INTELLI CC+FRT SONAR)
    PWRPKG(P/W+P/L+CC)
    select  txt,
            regexp_substr(
                          txt,
                          '(.*\()*([^+)]+)',
                          1,
                          column_value,
                          null,
                          2
                         ) element,
            column_value element_number
      from  table_x,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by level <= regexp_count(txt,'\+') + 1
                       as sys.OdciNumberList
      order by rowid,
               column_value
    TXT                                      ELEMENT    ELEMENT_NUMBER
    TECHPKG(INTELLI CC+FRT SONAR)            INTELLI CC              1
    TECHPKG(INTELLI CC+FRT SONAR)            FRT SONAR               2
    PWRPKG(P/W+P/L+CC)                       P/W                     1
    PWRPKG(P/W+P/L+CC)                       P/L                     2
    PWRPKG(P/W+P/L+CC)                       CC                      3
    SQL>  SY.

  • Need help on Regular expression and query

    Hi Guru's, Hope you all are doing great!.
    I have a scenario's where i need do insert the data into table.
    I have three scenarios :
    select 'Kodali,Raj,S' str from dual
    union
    select 'Alex Romano' from dual
    union
    select 'ppppp' from dual
    Alex Romano
    Kodali,Raj,S
    pppppNow what i want is .
    1. Alex Romano
    if there is space between the string then i want to insert into first name and last name columns
    2. Kodali,Raj,S
    if its a comma between the string then i want to insert into last name , first name and middle name
    3. if there is only one string then same insert into first name and last name.
    I wrote the query earlier to handle only comma and now i am trying but not able to use this all scenarios
    Can you please help me out.
    WITH t AS (
    select 'Kodali,Raj,S' str from dual
    union
    select 'Alex Romano' from dual
    union
    select 'ppppp' from dual
    select DECODE(trim(a),NULL,'a',trim(a)),DECODE(trim(b),NULL,'b',trim(b)),decode(trim(c),NULL,'c' ,trim(c))
    from
      SELECT max(decode(level,1,regexp_substr(str,'[^,]+',1,level))) a --INTO lFNAME
              , max(decode(level,2,regexp_substr(str,'[^,]+',1,level))) b --INTO lLNAME
              , max(decode(level,3,regexp_substr(str,'[^,]+',1,level))) c --INTO lMNAME      
       FROM   t
      CONNECT BY regexp_substr(str,'[^,]+',1,level) IS NOT NULL
      GROUP BY str
    ) ;Currently i am putting a b and c if its null.
    Thanks in advance!

    user590978 wrote:
    Hi Guru's, Hope you all are doing great!.
    I have a scenario's where i need do insert the data into table.
    I have three scenarios :
    select 'Kodali,Raj,S' str from dual
    union
    select 'Alex Romano' from dual
    union
    select 'ppppp' from dual
    Alex Romano
    Kodali,Raj,S
    pppppNow what i want is .
    1. Alex Romano
    if there is space between the string then i want to insert into first name and last name columns
    2. Kodali,Raj,S
    if its a comma between the string then i want to insert into last name , first name and middle name
    3. if there is only one string then same insert into first name and last name.
    I wrote the query earlier to handle only comma and now i am trying but not able to use this all scenarios
    Can you please help me out.
    WITH t AS (
    select 'Kodali,Raj,S' str from dual
    union
    select 'Alex Romano' from dual
    union
    select 'ppppp' from dual
    select DECODE(trim(a),NULL,'a',trim(a)),DECODE(trim(b),NULL,'b',trim(b)),decode(trim(c),NULL,'c' ,trim(c))
    from
    SELECT max(decode(level,1,regexp_substr(str,'[^,]+',1,level))) a --INTO lFNAME
    , max(decode(level,2,regexp_substr(str,'[^,]+',1,level))) b --INTO lLNAME
    , max(decode(level,3,regexp_substr(str,'[^,]+',1,level))) c --INTO lMNAME      
    FROM   t
    CONNECT BY regexp_substr(str,'[^,]+',1,level) IS NOT NULL
    GROUP BY str
    ) ;Currently i am putting a b and c if its null.
    Thanks in advance!It's never a popular suggestion, but why not fix the design to begin with?
    It's the year 2013, can a client not pass you a "complex" data structure consisting of first, middle and last name?
    Why rely on an unreliable construct when there are so many more usable way to achieve this?
    Cheers,

  • Please help with regular expression

    Hello,
    With the help of my previous posting answers, Re: Procedure to Extract multiple substring from a string , I updated the query. But, I am not getting desired answer in all case. Could you please help me out? My query is based on the previous posting. Any other simple way to achieve this?
    I will really appreciate it.
    select
           ltrim ( regexp_substr(txt, '\[(\w+)', 1, level), '[')      as id, /* id is number */
           ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '\w+-*\d*', 1, 1), ':')  as qid, /* Qid could be char/number/space any combination except ':' */
           ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '\w+', 1, 2), ':')      as num,
          to_date( ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '[^:]+', 1, 3), ':'),'MM/DD/YY')   as effdate
    from  (
                            select  '[10946:M100:N:][10947:Q1222:N:][38198:PPP-2:N:][13935:PPP-6:N:][38244:QQQ-4:Y:01/01/10]'     as txt
                            from     dual
            connect by level <= length(regexp_replace(txt, '[^[]'));I should get :
    ID             QID          NUM         EFFDATE
    10946     M100     N     
    10947     Q1222     N     
    38198     PPP-2     N     
    13935     PPP-6     N     
    38244     QQQ-4     Y     01-JAN-10But, getting
    ID             QID          NUM          EFFDATE
    10946     M100     N     
    10947     Q1222     N     
    38198     PPP-2     2     
    13935     PPP-6     6     
    38244     QQQ-4     4     01-JAN-10Thanks,

    Hi,
    So the num column is wrong, is that it?
    Describe what the num column should be. For example "num is the 3rd part of the :-delimited list enclosed in the square brackets".
    If that's what you want, then change the definition of num from
    ...                     ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '\w+', 1, 2), ':')      as num,to
    ...                      REGEXP_SUBSTR  ( REGEXP_SUBSTR ( txt
                                          , '[^]]+'
                                                , 1
                                       , LEVEL
                             , '[^:]+'
                             , 1
                             , 3
                             )       AS num,

  • Help with regular expression to find a pattern in clob

    can someone help me writing a regular expression to query a clob that containts xml type data?
    query to find multiple occurrences of a variable string (i.e <EMPID-XX> - XX can be any number). If <EMPID-01> appears twice in the clob i want the result as EMPID-01,2 and if EMPID-02 appears 4 times i want the result as EMPID-02,4.

    with
    ofx_clob as
    (select q'~
    <EMPID>1
    < UNQID>123456
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    <EMPID>2
    < UNQID>123457
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    <EMPID>1
    < UNQID>123458
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    ~' ofx from dual
    select '<EMPID>' || to_char(ids) || '(' || to_char(count(*)) || ')' multi_empid
      from (select replace(regexp_substr(ofx,'<EMPID>\d*',1,level),'<EMPID>') ids
              from ofx_clob
            connect by level <= regexp_count(ofx,'<EMPID>')
    group by ids having count(*) > 1
    MULTI_EMPID
    <EMPID>1(2)
    with
    ofx_clob as
    (select q'~
    <EMPID>1
    < UNQID>123456
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    <EMPID>2
    < UNQID>123457
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    <EMPID>1
    < UNQID>123456
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    <EMPID>2
    < UNQID>123456
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    <EMPID>1
    < UNQID>123458
    < TIMESTAMP>...
    < ADDRINFO>
    < TITLE>^@~*
    < FIRST>ABCD
    < MI>
    < LAST>EFGH
    < ADDR1>ADDR1
    < ADDR2>^@~*
    < CITY>CITY
    ~' ofx from dual
    select '<EMPID>' || listagg(to_char(ids) || '(' || to_char(count(*)) || ')',',') within group (order by ids) multi_empid
      from (select replace(regexp_substr(ofx,'<EMPID>\d*',1,level),'<EMPID>') ids
              from ofx_clob
            connect by level <= regexp_count(ofx,'<EMPID>')
    group by ids having count(*) > 1
    MULTI_EMPID
    <EMPID>1(3),2(2)
    Regards
    Etbin
    Message was edited by: Etbin
    used listagg to report more than one multiple <EMPID>

  • Need help in write a query

    Hi guys,
    i have two table. here in each table data like below
    tab1(id, app_id)
    id    app_id
    1     111
    2     222
    tab2(id,role_id)
    id      role_id
    1      900,901,902
    and one emp(id,empid) table.
    id      empid
    1       1234
    Now i want to display app_id, role_id which are assigned a perticular empid = 1234 and which saticifies the ID join condition.
    can any one help me on this
    Rgds,
    KLR

    Wowwww... where is my prev post?
    ok i repeat it :
    1) you need this?
    select e.empid, t2.role_id, t1.app_id from emp e
    join tab2 t2
    on t2.id = e.id
    join tab1 t1
    on t1.id = e.id
    SQL>
    19  /
         EMPID ROLE_ID         APP_ID
          1234 900,901,902        111
    2) or this?
    select empid,
           REGEXP_SUBSTR(role_id,'[^,]+',1,row_number() over(partition by empid order by empid)) val,
           app_id
      from (select e.empid, t2.role_id, t1.app_id
              from emp e
              join tab2 t2
                on t2.id = e.id
              join tab1 t1
                on t1.id = e.id
            connect by INSTR(t2.role_id, ',', 1, level - 1) > 0
                   and prior e.empid = e.empid
                   and prior sys_guid() is not null)
    EMPID    VAL    APP_ID
    1234    900    111
    1234    901    111
    1234    902    111
    Ramin Hashimzade

  • NEED HELP IN SQL HOMEWORK PROBLEMS

    I NEED HELP IN MY SQL HOMEWORK PROBLEMS....
    I CAN SEND IT VIA EMAIL ATTACHMENT IN MSWORD....

    Try this:
    SELECT SUBSTR( TN,
                   DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1) + 1),
                   DECODE( INSTR(TN, '#', 1, LEVEL) , 0 ,
                           LENGTH(TN) + 1, INSTR(TN, '#', 1, LEVEL) )
                   - DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1 ) + 1)
           ) xxx
    FROM (
        SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
    CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
    XXX                               
    234123                            
    1254343                           
    909823                            
    908232                            
    12345
    SELECT regexp_substr(tn, '[^#]+', 1, level) xx
    FROM (
        SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
    CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
    XX                                
    234123                            
    1254343                           
    909823                            
    908232                            
    12345 

  • Help with Nesting DECODE statements

    Hello. I'm trying to write a sql script to generate data report for payments. I need to incorporate specific conditions to SUPPRESS BLANK ADDRESS field lines. I am pulling Address_Line1, Address_Line2, Address_Line3, plus CITY, STATE, ZIP as Address_Line4. If any fields are blank, I need to suppress the blank line and move the other lines up.
    Here are my IF, THEN, ELSE conditions. There are 9 possible scenarios that I have identified. Can you please help me write this in SQL with the DECODE function?
    IF CITY||STATE||ZIP IS NULL               
    THEN AD1='12115 Rainbow Road', AD2='Hartford Heights, MO 60226'               
    ELSE               
         IF ADDRESS_LINE1 IS NULL          
              IF ADDRESS_LINE2 IS NULL     
                   IF ADDRESS_LINE3 IS NULL
                   THEN USE HOME ADDRESS
                   ELSE vAD1=ADDRESS_LINE3, vAD2=CITY||STATE||ZIP
                   ENDIF
              ELSE     
                   IF ADDRESS_LINE3 IS NULL
                   THEN vAD1=ADDRESS_LINE2, vAD2 = CITY||STATE||ZIP
                   ELSE vAD1=ADDRESS_LINE2, vAD2=ADDRESS_LINE3, vAD3=CITY||STATE||ZIP
                   ENDIF
              ENDIF     
         ELSE          
              IF ADDRESS_LINE2 IS NULL     
                   IF ADDRESS_LINE3 IS NULL
                   THEN vAD1=ADDRESS_LINE1, vAD2=CITY||STATE||ZIP
                   ELSE vAD1=ADDRESS_LINE1, vAD2=ADDRESS_LINE3, vAD3=CITY||STATE||ZIP
                   ENDIF
              ELSE     
                   IF ADDRESS_LINE3 IS NULL
                   THEN vAD1=ADDRESS_LINE1, vAD2=ADDRESS_LINE2, vAD3=CITY||STATE||ZIP
                   ELSE vAD1=ADDRESS_LINE1, vAD2=ADDRESS_LINE2, vAD3=ADDRESS_LINE3, vAD4=CITY||STATE||ZIP
                   ENDIF
              ENDIF     
         ENDIF          
    ENDIF               
    This what I've got so far...
    DECLARE
      vADR1
      vADR2
      vADR3
      vADR4
    BEGIN
    SELECT
    DECODE(
    END;
    /

    Hi,
    Here is the code along with some sample data. I substitute the "+" symbol for "/" becuase some addresses where using "c/o" in the address line.
    The address fields are null, as shown by the query below (ref query results below):
    SQL> select address_line1
      2  from po.po_vendor_sites_all
      3  where address_line1='3457 SOLUTIONS CENTER'
      4  and address_line2 is null;
    ADDRESS_LINE1
    3457 SOLUTIONS CENTERHere is the script:
    WITH got_delimited_list AS
    SELECT address_line1 || ' +' ||
    address_line2 || ' +' ||
    address_line3 || ' +' ||
    city ||', ' || state ||'  ' || zip AS delimited_list
    FROM po.po_vendor_sites_all
    SELECT REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 1) AS vad1
    , REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 2) AS vad2
    , REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 3) AS vad3
    , REGEXP_SUBSTR (delimited_list, '[^+]+', 1, 4) AS vad4
    FROM got_delimited_list;Sample of the data records returned:
    VAD1
    VAD2
    VAD3
    VAD4
    3457 SOLUTIONS CENTER
    CHICAGO, IL  60677-3004
    VAD1
    VAD2
    VAD3
    VAD4
    5172 EAGLE WAY
    CHICAGO, IL  60678-1517
    VAD1
    VAD2
    VAD3
    VAD4
    RETAILERS' OCCUPATION TAX
    SPRINGFIELD, IL  62796-0001
    VAD1
    VAD2
    VAD3
    VAD4
    DEPT. NUMBER 478150
    P.O. BOX 790100
    ST LOUIS, MO  63179-9933
    VAD1
    VAD2
    VAD3
    VAD4
    P.O. BOX 62251
    BALTIMORE, MD  21264-2251
    VAD1
    VAD2
    VAD3
    VAD4
    P.O. BOX 660481
    DALLAS, TX  75266-0481
    VAD1
    VAD2
    VAD3
    VAD4
    3525 PIEDMONT RD.
    BUILDING FIVE
    SUITE 300
    ATLANTA, GA  30305

  • Need help in finding the number of occurrences of a pattern.

    Hi All,
    I need help in finding the number of occurrences of a pattern in a table's column's data.
    Consider sample data - one row's column from a table:
    "S-S-S-A-S-S-P-S-S-B-S-A-P-S-S-C"
    My requirement is:
    I should get the count of S's which are immediately preceded by A or P.
    for the above data i should get count as 3+2+1=6 (S-S-S-A, S-S-P, S-A)
    The pattern data is stored as VARCHAR2 type.
    Thanks in advance,
    Girish G
    Edited by: Girish G on Jul 21, 2011 11:22 PM

    I am sure there exists a better way then this one:
    SQL> with dt as
      2  (select 'S-S-S-A-S-S-P-S-S-B-S-A-P-S-S-C' str from dual)
      3  SELECT SUM(Regexp_count(Regexp_substr(str, '(S\-?)+(A|P)+', 1,
      4                                     Regexp_count(str, '(S\-?)+(A|P)+') - (
      5                                                  LEVEL - 1 )), 'S')) len
      6  FROM   dt
      7  CONNECT BY LEVEL <= Regexp_count(str, '(S\-?)+(A|P)+')
      8  /
           LEN
             6

  • Help with Understanding Regular Expressions

    Hello Folks,
    I need some help in understanding the Regular Expressions.
    -- This returns the Expected string from the Source String. ", Redwood Shores,"
    SELECT
      REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA,aa',
                    ',[^,]+,', 1, 1) "REGEXPR_SUBSTR"
      FROM DUAL;
    REGEXPR_SUBSTR
    , Redwood Shores,
    However, when the query is changed to find the Second Occurrence of the Pattern, it does not match any. IMV, it should return ", CA,"
    SELECT
      REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA,aa',
                    ',[^,]+,', 1, *2*) "REGEXPR_SUBSTR"
      FROM DUAL;
    REGEXPR_SUBSTR
    NULLCan somebody help me in understanding Why Second Query not returning ", CA,"?
    I did search this forum and found link to thread "https://forums.oracle.com/forums/thread.jspa?threadID=2400143" for basic tutorials.
    Regards,
    P.

    PurveshK wrote:
    Can somebody help me in understanding Why Second Query not returning ", CA,"?With your query...
    SELECT
      REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA,aa',
                    ',[^,]+,', 1, *2*) "REGEXPR_SUBSTR"
      FROM DUAL;You are looking for patterns of "comma followed by 1 or more non-comma chrs followed by a comma."
    So, let's manually pattern match that...
    '500 Oracle Parkway, Redwood Shores, CA,aa'
                       ^               ^
                       |               |
                               |
                               |
                      Here to here is the
                      first occurence.So the second occurance will start searching for the same pattern AFTER the first occurence.
    '500 Oracle Parkway, Redwood Shores, CA,aa'
                                        ^
                                        |
    i.e. the search for the second occurence starts heretherefore the first "," from that point is...
    '500 Oracle Parkway, Redwood Shores, CA,aa'
                                           ^
                                           |
                                          hereand there is nothing there matching the pattern you are looking for because it only has the
    "comma follwed by 1 or more non-comma chrs"... but it doesn't have the "followed by a comma"
    ...so there is no second occurence,

Maybe you are looking for

  • HP Color LaserJet CM2320nf MFP

    We are having an issue with the above mentioned printer.....the printer is networked so multiple users can use it. The issue is that one user in particular is unable to print in color although the printer is set at the default settings and all the ot

  • Error in Java console

    Hi experts when I run Network UI Element to presnt org Chart I get in  IE->Tools->Java Console the follow error: <<<--- Applet.destroy(): AppWin0/JNET --->>> <<<--- EXIT for AppWin0/JNET --->>> java.lang.NullPointerException      at sun.plugin.javasc

  • Hide every toolbars in Acrobat Reader

    Hi everybody !!!! I'm currently working on a C# application in using Acrobat Reader to display PDF. I would like to display the only document and hide every tools (including the scollbar) to add my own tools. I found setShowScrollbars() and setShowTo

  • Zooming inconsistency with links in pdf

    I have an interactive pdf set up containing different chapters & vast amounts of links. I have set the pdf up to 100% zoom. When I open it all is fine, but as soon as I begin to click through the document the page zooming becomes erratic. I have trie

  • ESS- Defining Resources.

    Hi All, When you define the Resources for ESS implementation at IMG>Personnel Management>Employee Self Service>Home Page for Employee Self Services>Resources>Define Resources path, you have something called "URL of PCD Pages" field. What is the signi