Skip to Main Content
  • Questions
  • How does Connect by Level work inside INSTR

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, magesh.

Asked: September 28, 2015 - 5:40 am UTC

Last updated: September 29, 2015 - 6:53 am UTC

Version: 11g

Viewed 10K+ times! This question is

You Asked

Hi Tom,
Hope your doing good!

The below query returns the right output, but my question is as far as my understanding here in the first check itself i.e. (Level-1) the condition fails(1-1). but I dont know how does it proceeds further,can you explain how does connect by level works in this scenario.

WITH DATA AS
( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
)
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str,level
FROM DATA
CONNECT BY instr(str, ',', 1, LEVEL-1) > 0;


output
STR LEVEL
---------------------------------------- ----------
word1 1
word2 2
word3 3
word4 4
word5 5
word6 6

6 rows selected

If I put only level and level+1 as below the output is decreased am really confused about how it works. Please explain me along with its working flow in detail.

WITH DATA AS
( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
)
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str,level
FROM DATA
CONNECT BY instr(str, ',', 1, LEVEL) > 0;


output
STR LEVEL
---------------------------------------- ----------
word1 1
word2 2
word3 3
word4 4
word5 5

WITH DATA AS
( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
)
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str,level
FROM DATA
CONNECT BY instr(str, ',', 1, LEVEL+1) > 0;


output
STR LEVEL
---------------------------------------- ----------
word1 1
word2 2
word3 3
word4 4

Kindly explain me how it works in detail. Thanks in advance.

and Connor said...

The hint might be in what a "normal" CONNECT BY looks like

connect by prior COL1 = COL2

Notice that we are looking at the *prior* value. So when the connect by is 'evaluated' we are onto the "next" value and looking "backwards". So (conceptually) we output row 1 (with level = 1) then move on to the *next* level (=2) and perform the connect by

Hope this helps

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

magesh sekar, September 29, 2015 - 4:28 am UTC

Hi,

Here in this query we are not using Prior keyword, then as you advised, I can assume that even though we are not using prior keyword will it assume prior and use the above value?

I am sorry that I cant understand still, can you please elaborate the process in steps, because if I use the below one in level the output is out of range since it becomes zero(2-2)
am confused about how it evaluates.

WITH DATA AS
( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
)
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str,level
FROM DATA
CONNECT BY instr(str, ',', 1, LEVEL-2) > 0;

also I am not getting appropriate result if i Put level+1 or level+2 the output value is reduced as below..


Please explain me in detail, I am not using prior keyword here so what happens in each scenario.

WITH DATA AS
( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
)
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str,level
FROM DATA
CONNECT BY instr(str, ',', 1, LEVEL+1) > 0;


output
STR LEVEL
---------------------------------------- ----------
word1 1
word2 2
word3 3
word4 4


Connor McDonald
September 29, 2015 - 6:53 am UTC

think of it this way:

we are processing the row:
- level = 1
- we calculate trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str,level
- now we need to see if this row is a parent to other rows (ie, oracle thinks we are doing a hierarchy query)
- so now level = 2, and *now* we evaluate the connect by
- it evaluates to true, so we continue

We never had to evaluate the 'connect by' expression until level had moved *past* 1


magesh sekar, September 30, 2015 - 9:44 am UTC

Hi,
Thank you very much for your kind answers, I understood now.



Regards,
Magesh