Find the last value from a comma separated column
I have comma separated text in a given column. Is there a way to get the last, second last, and third last value?
select mnumber from mytable limit 2;
x, y, z, 1, 2, 3
a, b, c, d, e, f, g, h, 7, 8, 9
The values ββreturned from this column should look something like this:
col3, col2 col1
1 2 3
7 8 9
This does not work:
split_part(mnumber,',',-1) as last_col
It would give me back what I need if minus values ββwere allowed in the split_part function.
+3
source to share
2 answers
this will give you the last value for the column
select SPLIT_PART(mnumber, ',', (len(mnumber)/2+1)) from mytable
this will give the third last value for the column
select SPLIT_PART(mnumber, ',', ((len(mnumber)/2)-1)) from mytable
This will give the second last value for the column
select SPLIT_PART(mnumber, ',', (len(mnumber)/2)) from mytable
+1
source to share