0

How can I split a string by comma using oracle sql? Here I have a column which has values like below

123Lcq
Lf32i
jkp32m

I want to split it by comma

1,2,3,L,c,q
L,f,3,2,i
j,k,p,3,2,m
2
  • Does your data have multi-line strings? Commented Apr 10, 2016 at 12:55
  • Ronnie: you posted a related question yesterday. After you received answers, you posted the following comment: "Thank you so much, it is the most amazing code I have ever seen. Amazing Oracle function. But what a shame I am using Netezza as my database. – Ronnie Lu 18 hours ago." In a follow-up comment I asked you why that question was tagged "Oracle" if you don't use Oracle. Now I see you posted this question ALSO tagged as "oracle." Please explain. Commented Apr 10, 2016 at 22:13

2 Answers 2

1

You can achieve the desired output using REGEXP_REPLACE:

SELECT
    rtrim(regexp_replace(text, '(.)', '\1,'), ',') result
FROM (
    SELECT '123Lcq' text FROM dual UNION ALL
    SELECT 'Lf32i'  text FROM dual UNION ALL
    SELECT 'jkp32m' text FROM dual)
Sign up to request clarification or add additional context in comments.

Comments

1

You could use regexp_replace:

SELECT substr(regexp_replace(mycol, '(.)', ',\1'), 2)
FROM   mytable

The regular expression finds every character, and those matching characters are then all prefixed with commas. Finally a simple substr is used to eliminate the first comma.

Note that trimming commas could be an alternative to substr, but the behaviour is different when the original value already has commas at the end of the string: when trimming, you also trim away these original commas.

Comments

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.