0

How can a word like 'ORACLE' be displayed as rows like this, using only one SQL statement?

Word

O

R

A

C

L

E

1 Answer 1

3

You could use the CONNECT BY condition:

SELECT SUBSTR('ORACLE', level, 1)
FROM dual
CONNECT BY level <= LENGTH('ORACLE');

Edit 1: As per Alex Poole's suggestion, I replaced regexp_substr('ORACLE', '.', 1, level) IS NOT NULL with level <= LENGTH('ORACLE');

Edit 2: I replaced regexp_substr with substr

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

I have gone through couple of other ways of hierarchical querying also... Seems these iterations can be done with CONNECT BY only... Any suggestion on whether it can be done with decode?
You could do it with decode as well, but you would still have to use CONNECT BY. But you could also do it with SUBSTR (without the need of regexp). See my updated answer.
Got an another way which can be used in any version of ORACLE.. SELECT substr('ORACLE', rownum, 1) FROM all_objects WHERE rownum <= (SELECT length('ORACLE') FROM dual)
Yes, but this will not work if there are less than 6 objects; still, it's an alternative for older versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.