Use of RegExp and connect by level in Oracle SQL

Hello

Can someone help me to know about the use of regular expressions and the concept connect by level in oracle SQL?

Thanks adavance

Prakash wrote:
Hello

select regexp_substr(str, '[^,]+', 1, level) val
from (select 'a,b,c,d' str from dual)
connect by level <= length(str) - length(replace(str,','))+1

i  tried my level to understand it but couldn't

Please explain  how it works

Please only put code in tags code, otherwise it's hard to see what you're asking.

In response to your question look at the query...

SQL> select regexp_substr(str, '[^,]+', 1, level) val
  2      from (select 'a,b,c,d' str from dual)
  3   connect by level <= length(str) - length(replace(str,','))+1
  4  /

VAL
-------
a
b
c
d

OK, so clearly it's split the string anywhere where there is a comma, then, how he does it.

Firsly we look at connect by clause it when we use it as a generator of line...

SQL> select level from dual connect by level <= 4;

     LEVEL
----------
         1
         2
         3
         4

Now, look at how the number 4 is calculated from the string...

SQL> select length('a,b,c,d') - length(replace('a,b,c,d',','))+1 from dual;

LENGTH('A,B,C,D')-LENGTH(REPLACE('A,B,C,D',','))+1
--------------------------------------------------
                                                 4

... looking at the length of the string and the length of it without comma, we can determine how many commas are in the chain and the surmize that there must be 1 more value there are commas.

Now look at the regular expression...

SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 1) from dual;

R
-
a

SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 2) from dual;

R
-
b

SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 3) from dual;

R
-
c

SQL> select regexp_substr('a,b,c,d', '[^,]+', 1, 4) from dual;

R
-
d

The 4th parameter of the regexp_substr function is the 'presence' of the search string to return.

The actual regular expression itself is looking for 1 or more characters (sign +) who are not (the ^ symbol) a comma.

Is that more clear?

Tags: Database

Similar Questions

  • I use Windows Vista and that you need to download SQL Server 2005 Express Service Pack 4. I get an error code of 64 C. Please tell me what to do?

    I use Windows Vista and that you need to download SQL Server 2005 Express Service Pack 4. I get an error code 64 C help, please.

    Hello

    I suggest you to try the steps below and check if it helps.

    Method 1: Try to reset components to update Windows from the following link, which would help you to install the updates of windows. http://support.Microsoft.com/kb/971058

    Method 2: If the problem persists, then try to set up your computer in a clean boot state, and check to see if the same problem happens.

    By setting your boot system minimum state helps determine if third-party applications or startup items are causing the problem.

    How to troubleshoot a problem by performing a clean boot in Windows Vista or Windows 7:
    http://support.Microsoft.com/kb/929135

    Note: After the boot minimum troubleshooting step, follow step 7 in the link provided to return the computer to a Normal startup mode.

    Method 3: Also try to install the updates manually from the link below.

    http://www.Microsoft.com/download/en/details.aspx?displaylang=en&ID=184

    You can also consult the following link for more information:

    Troubleshoot problems with installing updates.

    http://Windows.Microsoft.com/en-us/Windows-Vista/troubleshoot-problems-with-installing-updates

    Hope this information is useful.

  • 0 and the decimal point in Oracle SQL

    Hello


    IM using Oracle Sql developer 'Oracle Database 11g Enterprise Edition Release 11.1.0.6.0' (I'm fairly new), im having trouble inserting '0 before 1' in the table, as well as with the decimal point
    I created a table with the following:

    CREATE TABLE employee
    (Employe_id number (4,0) NOT NULL primary key,)
    L_Name varchar2 (15).
    What varchar2 (10),
    Number of Pay_Rate (5);

    Then tried to insert this: insert into employee
    values ('003', 'Clarke', 'Michael', '13 h 00');
    which has been a success, but when I used the query select it to display the table, the result was not the same

    result:
    Employee_id L_Name what Pay_Rate
    1 13 Michael Clarke


    How can I ensure that the result looks like this (001 and 13 h 00):
    Employee_id L_Name what Pay_Rate
    001 Micheal Clarke 13.00

    Edited by: 936867 may 25, 2012 20:15

    Edited by: 936867 may 25, 2012 20:18

    936867 wrote:
    Sorry, I wanted to ask "what needs to be changed in the create table statement, so when I introduced through the 'Insert into' data is presented as above after the select query. I want the data to show as entered when I look at the table, I know that can use the select query to display data in the desired format.

    Then do not define the columns as a NUMBER, but as strings of VARCHAR2,
    but realize that you can't do arithmetic on strings.

    NUMBERS have no beginning or ending zeros but bites can contain what you want.

  • How to use the Pivot function for range group in oracle SQL

    Hello

    Hello!!!

    I need to display the data in the format below. There are 2 columns 1 is State and another is rate.

    State < 100 100-199, 200-299 300-399 400-499, 500-599 600-699 700-799 800-899 900-999 > = 1000 Total
    AK 1 2 0 4 1 4 4 35 35 4 1 25
    AL 0 0 2 27 10 17 35 2 2 35 0 103
    AR 0 0 1 0 0 2 2 13 13 2 0 6
    AZ 0 1 2 14 2 14 13 3 3 13 0 57
    CA     0     0     1     6     2     7     3     4     4     3     0     34

    Developed the sub query but unable to use the beach on the pivot function. Please help on this.

    (select (SELECT short_description
    OF CODE_VALUES
    WHERE CODE_TYPE_CODE = ad. STATE_TYPE_IND_CODE
    AND VALUE = ad. STATE_CODE
    ) STATE,
    Nr.rate
    N-NEUTRAL
    c contacts,
    announcement of addresses,
    xref_contacts_addresses xca,
    neutral_rates nr
    where n.contact_id = c.contact_id
    and n.address_id = ad.address_id
    and xca.address_id = ad.address_id
    and xca.contact_id = c.contact_id
    and nr.contact_id = n.contact_id
    and nr.rate_frequency = 'HOUR')

    Like this

    with t
    as
    (
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 67 value from dual union all
    select 'AL' state, 23 value from dual union all
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 78 value from dual union all
    select 'AL' state, 34 value from dual union all
    select 'AL' state, 4 value from dual union all
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 15 value from dual union all
    select 'AZ' state, 6 value from dual union all
    select 'AZ' state, 123 value from dual union all
    select 'AZ' state, 123 value from dual union all
    select 'MA' state, 23 value from dual union all
    select 'MA' state, 120 value from dual union all
    select 'MA' state, 456 value from dual union all
    select 'MA' state, 11 value from dual union all
    select 'MA' state, 24 value from dual union all
    select 'MA' state, 34 value from dual union all
    select 'MA' state, 87 value from dual union all
    select 'MA' state, 23 value from dual union all
    select 'MA' state, 234 value from dual union all
    select 'MA' state, 789 value from dual union all
    select 'MH' state, 54321 value from dual
    )
    -- End of test data
    select state,
           nvl(count(decode(value, 0, 0)), 0) "<100",
           nvl(count(decode(value, 1, 1)), 0) "100-199",
           nvl(count(decode(value, 2, 2)), 0) "200-299",
           nvl(count(decode(value, 3, 3)), 0) "300-399",
           nvl(count(decode(value, 4, 4)), 0) "400-499",
           nvl(count(decode(value, 5, 5)), 0) "500-599",
           nvl(count(decode(value, 6, 6)), 0) "600-699",
           nvl(count(decode(value, 7, 7)), 0) "700-799",
           nvl(count(decode(value, 8, 8)), 0) "800-899",
           nvl(count(decode(value, 9, 9)), 0) "900-999",
           nvl(count(decode(value, 10, 10)), 0) ">=1000"
      from (
              select state, case when value < 100 then 0
                                 when value between 100 and 199 then 1
                                 when value between 200 and 299 then 2
                                 when value between 300 and 399 then 3
                                 when value between 400 and 499 then 4
                                 when value between 500 and 599 then 5
                                 when value between 600 and 699 then 6
                                 when value between 700 and 799 then 7
                                 when value between 800 and 899 then 8
                                 when value between 900 and 999 then 9
                                 when value >= 1000 then 10
                            end value
                from t
           )
     group
        by state
    
  • Howa allows you to configure a connection on MAC developer Oracle SQL on a server

    My question is how to configure a connection on a Mac to Oracle on our Windows Server.

    I downloaded SQL Server and can open it. I try a new connection. On Windows, I know that I have a tnsnames file that identifies the
    Server. On my PC, the entry for Oracle looks like this:

    ORCL =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = server1.iowa.uiowa.edu) (PORT = 1521))
    )
    (CONNECT_DATA =
    (SERVICE_NAME = ORCL)
    )
    )

    On MAC, I tried to make connection type 'Basic', then put in what follows:
    HostName: server1.iowa.uiowa.edu
    Port: 1521
    SID: ORCL

    When I test this situation, I get "unknown host specified. I tried to log on to the network. Then, I get:
    "The network adapter could not establish the connection."

    I also tried to go to TNS, but do not get anything on the menu drop down. On my PC, I know that I
    a tnsnames.ora file that it refers, but don't know how to put this on a Mac.

    Could someone please explain to me how to set up the connection?

    Sorry if I suggest what is obvious, but you can ping the server from the Mac? Of course it can access the network and the server in the same way?
    Maybe that's just a problem of DNS, so also try to put the IP address instead.

    Hope that helps,
    K.

  • Connect by PRIOR vrs Connect by LEVEL

    Hello

    I'm trying to understand the difference between CONNECT BY PRIOR and CONNECT BY LEVEL

    I know that CONNECT BY PRIOR is basically specifying a hierarchy and which column / field is the parent (using the PRIOR)

    for example

    SELECT employe_id, employee_name, level

    Employees

    CONNECT BY PRIOR employee_id = manager_id;


    Q1. What is CONNECT BY LEVEL (for example as shown below) practice? What type of relationship normally described? (below, it is just producucing a series of integers)

    SELECT the level

    OF the double

    CONNECT BY LEVEL < = 10;

    T2. Use LEVEL with CONNECT BY in this way, a subversive use of the CONNECT IN knowledge is really intended to be used this way?

    any advice appreciated,

    Jim

    Hi, Jim,.

    Jimbo wrote:

    Hello

    I'm trying to understand the difference between CONNECT BY PRIOR and CONNECT BY LEVEL

    I know that CONNECT BY PRIOR is basically specifying a hierarchy and which column / field is the parent (using the PRIOR)

    for example

    SELECT employe_id, employee_name, level

    Employees

    CONNECT BY PRIOR employee_id = manager_id;

    Sometimes, CONNECT BY using hierarchical data, such as a tree.  In these cases, it may be useful to use the terns as "parent" and "child."  The query above is one of these cases.

    In more general terms, CONNECT BY returns the union (UNION ALL, specifically) of lines found by the START WITH clause (LEVEL = 1) and lines found by the clause CONNECT BY (LEVEL > 1).  If there is no START WITH clause, every row in the table will appear on LEVEL = 1.

    If there is no line on LEVEL = N, then Oracle research lines that meet the CONNECT BY clause, to search for the lines that are connected to each line level = N, for put the LEVEL N + 1.  If the CONNECT BY clause is set to TRUE for a given row, while the rank will appear on level N + 1 =.  Often (but not always) the CONNECT BY clause uses the PRIOR operator.  After PRIOR consent refers to something = N level.

    Q1. What is CONNECT BY LEVEL (for example as shown below) practice? What type of relationship normally described? (below, it is just producucing a series of integers)

    SELECT the level

    OF the double

    CONNECT BY LEVEL<>

    Since there is no START WITH clause, each line of the double table will be LEVEL = 1.

    The CONNECT BY clause means that CONNECT BY always means: If there is a line at = N, then it will be connected to any line that fills the CONNECT BY conditions.

    So that the lines will be added to the level = 2?  All the lines where condition 2<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">

    What lines will be added to the level = 3?  All the lines where condition 3<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">

    ...

    What lines will be added to the level = 10?  All the lines where the condition of 10<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="10." 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">

    What lines will be added to the level = 11?  All the lines where condition 11<= 10="" is="" true,="" so="" no="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">

    Since no rows are added to the LEVEL = 11, CONNECT BY query stop right there.

    Q2. Use LEVEL with CONNECT BY in this way, a subversive use of the CONNECT IN knowledge is really intended to be used this way?

    You can think of it as being subversive.  It was certainly not how planners and the deveopers at Oracle for CONNECT BY work in versions 2 to 8; you got an error if you tried in these versions.  It is also an exception to the rule that no line can be his own ancestor.

  • Hi there, I been using MacBook Air and IPad2 for awhile. Recently, I lost my MacBook Air and am interested open ICloud to check my folder/old records. Can I get directions to the opportunity to see my files in I have the cloud.

    Hi there, I been using MacBook Air and IPad2 for awhile. Recently, I lost my MacBook Air and am interested open ICloud to check my folder/old records. Can I get directions to the opportunity to see my files in I have the cloud.

    I hope that you are not in a surprise but a MacBook Air not backup to iCloud. It can record data in iCloud drive (if you saved it specifically to this place) and can synchronize Photos, Contacts, etc to iCloud if you had set it to do. You can use any computer and connect to https://www.icloud.com/ to see what is stored. I hope this is good news and that it contains what you hope.

  • Database on AIX and Windows patch level

    Hi guys

    I have a problem and would like to have your advice.
    I currently have my DB 10.2.0.3 put us on AIX 5.3 and exact patch level is

    Oracle Database 10g 10.2.0.1.0
    Oracle Database 10g Release 2 Patch Set 2 10.2.0.3.0
    There are 2 products in this House of Oracle.

    Interim plates (3):

    Patch 4684209 : applied on Mon 01 June at 19:09:01 GST 2009
    Created on April 13, 2007, 00:30:43 pm PST8PDT
    Bugs fixed:
    4684209

    Patch 5556081 : applied on Wed Apr 15 11:13 GST 2009
    Created November 9, 2006, 22:20:50 hrs PST8PDT
    Bugs fixed:
    5556081

    Patch 5557962 : applied on Wed Apr 15 11:12:47 GST 2009
    Created November 9, 2006, 23:23:06 hrs PST8PDT
    Bugs fixed:
    4269423, 5557962, 5528974


    My application provider advises me that my patch on AIX level must be equivalent to the following windows patch level.

    "ORACLE 10.2.0.3 PATCH BUG 8 FOR MICROSOFT WINDOWS, Chapter 1. 32 BIT"; Oracle Metalink patch number is 6116131.

    Concerning
    Kam

    According to the Metalink notes Critical Patch Update availability for Oracle and Middleware server information for product ID 432873.1* the equivalent of the patch 6116131 for Windows is 6079591 for AIX5L.

    Nicolas.

  • Why hp disables the use of scanning and faxing because of the low level of ink on my photosmart c4385?

    Why hp disables the use of scanning and faxing because of the low level of ink? Printer says print cartridge missing or not detected. Doesn't make sense to me because the ink has absolutely nothing to do with the other functions. Since I bought this all in one I replace ink twice and I only printed about 10 pieces of paper for nine, making these extremely expensive 10 sheets!

    try reseating the cartridges once more & switch off the printer and remove power & other cables connected to the printer & press & hold the power for 20 seconds & then reconnect all cables back & then turn on the pritner & try to use that will solve the problem...

  • Start - and connect SQL to the lowest level in the tree or the hierarchy

    Hello

    In a tree structure as shown below how can I get all the child (tree) level lowest records as a - E, H, G, and D.
    A a B, C, D to level 2
    B E and F at level 3, and C G at level 3
    F a H level 4

    A
    | l | l | l
    B C D
    | l | l | l
    E F G
    | l
    H

    I have used start with and coonect by to give all levels of the tree as follows: -.
    SELECT task_id, parent_task_id, top_task_id, level
    OF pa_tasks
    WHERE project =: p
    START WITH parent_task_id IS NULL
    CONNECT BY PRIOR task_id = parent_task_id

    Thank you

    Published by: user10648285 on October 17, 2011 23:42

    Published by: user10648285 on October 17, 2011 23:42

    Published by: user10648285 on October 17, 2011 23:42

    Published by: user10648285 on October 17, 2011 23:43

    Oracle a [url http://download.oracle.com/docs/cd/E11882_01/server.112/e26088/pseudocolumns001.htm#i1009313] nickname for hierarchical queries:

    SELECT task_id,parent_task_id, top_task_id,level
    FROM pa_tasks
    WHERE project_id = :bind_variable
    and connect_by_isleaf = 1
    START WITH parent_task_id IS NULL
    CONNECT BY PRIOR task_id = parent_task_id
    
  • Connect by level using the count of the records collection

    Hello:

    PROBLEM:
    ...
    ), (select rownum number months of double connect by level < = Cnt)

    Causes not valid ORA-00904 identifier. Why can I not use "Cnt" of my main as request by using a constant in the works?

    The value of count is determined for each date range in the section, I return the records that I need?

    Thank you, Sidney

    BACKGROUND:
    I have to be able to display a list of tax returns to my users and the status of these statements. There's no physical feedback so it is necessary to create data dynamically records using appropriate selection. It's not hard and I thought that would be enough to use a connection by level to give me the date, statements of information so that I can calculate and display the individual. However oracle gives me an ORA-00904 when I try to send the "Cnt" value to connect by level. Everything works fine when I provided a constant instead of "CNT". The "CNT" value is determined by a complex process that calculates start and end for several types of return dates, etc as well as the number of periods and frequency of production. The data must be generated dynamically using a master record, which then gives the history of coding in which my collection of basic records is selected. Here is the result of this process:

    TaxpayerNo, TaxClass TaxCode, FilingFequency, StartDate, StopDate, Cnt, frequency

    10 S 1 M 12-18 6/1/2007-11/30/2008
    10 S 2 M 12 10 30/11/2008-9/30/2009
    10 S, 2 Q 4 3 11/30/2010-8/18/2011
    L 10 8 6/1/2007 9/30/2009 3 1
    L 10 8 11/30/1 / 2010 8/18/2011 1

    According to the results, I have needs a record for each every month, quarter, etc. ie:

    10 S 1 M 6/1/2007 11/30/2008 18 12 6/1/2007
    10 S 1 M 6/1/2007 11/30/2008 18 12 7/1/2007
    10 S 1 M 6/1/2007 11/30/2008 18 12 8/1/2007
    10 S 1 M 6/1/2007 11/30/2008 18 12 9/1/2007
    ...
    10 S 2 M 30/11/2008-9/30/2009 10 12 11/30/2008
    10 S 2 M 30/11/2008-9/30/2009 10 12 12/30/2008
    ...
    etc.

    DOES NOT WORK
    Select y.*, MonthNo, Add_Months(StartDate,MonthNo*Frequency) in)
    Select x.*,
    (case when FilingFrequency = am' then Ceil (Months_Between (StopDate, StartDate)))
    When FilingFrequency = 'Q' then Ceil (Months_Between(StopDate,StartDate)/3)
    When FilingFrequency = 'A' Ceil (Months_Between(StopDate,StartDate)/12) then
    0 otherwise
    end) NTC,
    (case when FilingFrequency = am' then 1)
    When FilingFrequency = 'Q' then 3
    When FilingFrequency = 'A' then 12
    end) frequency
    de)
    ... a code complex to calculate values of start and stop dates required above...
    ) x
    ), (select rownum number months of double connect by level < = Cnt)
    ERROR MESSAGE
    The result is ORA-00904: "CNT": invalid identifier. I do not get an error if I use a constant:

    WORK WITH THE HELP OF A CONSTANT, BUT MUST HAVE THE VALUE OF THE ACTUAL CNT
    ... Same code for generating data...
    ), (select rownum number months of double connect by level < = 3)

    How can I get it works using the "CNT" value instead of a constant?

    A technique like this should solve your problem.

    TUBBY_TUBBZ?with data (col1, cnt) as
      2  (
      3    select 1, 3 from dual
      4      union all
      5    select 2, 2 from dual
      6  )
      7  select
      8    d.col1,
      9    t.column_value
     10  from
     11    data  d,
     12    table(cast(multiset(select level from dual connect by level <= d.cnt) as sys.OdciNumberList)) t;
    
                  COL1       COLUMN_VALUE
    ------------------ ------------------
                     1                  1
                     1                  2
                     1                  3
                     2                  1
                     2                  2
    
    5 rows selected.
    
    Elapsed: 00:00:00.00
    TUBBY_TUBBZ?
    

    Contrary to what you have now, which is basically this

    TUBBY_TUBBZ?with data (col1, cnt) as
      2  (
      3    select 1, 3 from dual
      4      union all
      5    select 2, 2 from dual
      6  )
      7  select
      8    d.col1,
      9    level
     10  from
     11    data  d
     12  connect by level <= d.cnt;
    
                  COL1              LEVEL
    ------------------ ------------------
                     1                  1
                     1                  2
                     1                  3
                     2                  2
                     1                  3
                     2                  1
                     1                  2
                     1                  3
                     2                  2
                     1                  3
    
    10 rows selected.
    
    Elapsed: 00:00:00.00
    TUBBY_TUBBZ?
    
  • When I have my browser "zoom in", it does not hold the zoom of the following view and I have to "re-zoom, he used to stay at a zoom level, ideas chosen?

    I used when I zoomed view using wheel Ctrl and mouse the next page would be at the sam zoom elevel. Now, I have to re-zoom whenever I open a page.

    The function Zoom of Page Firefox does a level of field by field to save favorite settings level users zoom, there is no default Page Zoom level setting in Firefox.

    Try the extension Default FullZoom Level:

    https://addons.Mozilla.org/en-us/Firefox/addon/6965

    Or the NoSquint extension:

    https://addons.Mozilla.org/en-us/Firefox/addon/2592/

  • Satellite L850 - unable to use Wi - Fi and Bluetooth connection.

    Hello

    I have a model of Satellite L850 Y-3110.

    Recently I formatted my laptop and installed a window Pro 8.1 instead of my pre installed Windows 8 and now, I'm not able to use my WiFi and blutooth.

    Bluetooth is not find a unit close to everything and a Wi - Fi connection does not work.
    I tried to download driver WLAN sites EU/AU, but it displays error after 30-35% of download.
    try to download this
    http://www.Toshiba.EU/innovation/Dow...verLanguage=42

    I tried the link and it works perfectly for me. I was able to download the driver. Try again please.
    It should work for you too.

    Of course, you must also download and install battery BT, driver of Toshiba and Toshiba function system key utility that allow you the feature of the F keys. Using specific F key, you will be able to enable or disable the wireless network adapter.

  • How many phone numbers can be connected simultaneously, use the most and merge buttons

    How many numbers phones can be connected at the same time, use the most and fusion of the buttons after you make the first call?

    It depends on your operator.

    It's been a few years since I've really looked into it, but I think that AT & T supports up to 5 concurrent calls. I know that other major U.S. carriers support less, but I don't remember the exact numbers. I want to say that Verizon and Sprint are in charge 3 and T-Mobile supports 2, but my memory on this subject is blurred.

    Contact your operator for more information.

  • Can I use an adapter of lightning and HDMI with my iPhone cable and connect it to my TV without messing up my AT &amp; T U-verse services?

    Can I use an adapter of lightning and HDMI with my iPhone cable and connect it to my TV without messing up my services AT & T U-Verse?

    Why have something to do with your Uverse services?

Maybe you are looking for