Add string to existing row separated by comma in sql

253 Views Asked by At
id  value
1   a
2   b   
3   c

How do i add second value 'z' to id=1 (separated by comma)?

id  value
1   a,z
2   b   
3   c

and how to remove the 'z' now if i have that final table?

3

There are 3 best solutions below

2
Gordon Linoff On

You can use update:

update t
    set value = concat(value, ',z')
    where id = 1;
0
Courageous Chronicler On

To answer your secondary question, yes.

If you run Select value from table where id = 1 it will return a,z. that means that if you are going to use it again in queries, you will quite possibly need to utilize a Split() type function, dependent on what you're doing with it.

0
rushan On

The best and simplest way to do this is the following query according to me :

update table1 set value = concat(value,'z') where id = 1

where : Table1 is the name of your table.