How to extract all values except the last value in a string separated by comma in sql

Joined
Oct 24, 2013
Messages
43
Reaction score
0
I am think of SUBSTRING_INDEX but dont know how to extract all values except the last value in a string separated by comma

e.g :
value is "aaa,bbb,ccc,ddd"
desired result is "aaa,bbb,ccc"

e.g :
value is "a1,b2,c3,d4,e5"
desired result is "a1,b2,c3,d4"
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
You have arrA and want arrB which is arrA without the last element:
arrB = arrA.pop();
 
Joined
Oct 24, 2013
Messages
43
Reaction score
0
You have arrA and want arrB which is arrA without the last element:
arrB = arrA.pop();
Hey I was looking for this and I figured it out with the following code:
SELECT SUBSTRING('aaa,bbb,ccc,ddd', 1, LENGTH('aaa,bbb,ccc,ddd') - LOCATE(',', REVERSE('aaa,bbb,ccc,ddd'))) AS result;
SELECT SUBSTRING('a1,b2,c3,d4,e5', 1, LENGTH('a1,b2,c3,d4,e5') - LOCATE(',', REVERSE('a1,b2,c3,d4,e5'))) AS result;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top