How to selectively remove a particular value from a column which contains more than one value each separated...
up vote
0
down vote
favorite
for example, consider a table like below (table name=accounts)
account owner_ids
PA account sa123,rt458,hf678,de348f
RA account gg678n,fk980,rt458,hf678
Here, I want to remove owner_ids "rt458" and "hf678"
from the entire table and after removing the commas should be perfect (I mean after removing also the remaining owner_ids should be separated by comma as before and there should not be any unnecessary comma in the column)
Any kind of help is appreciated, Help me out guys
mysql sql database oracle plsql
add a comment |
up vote
0
down vote
favorite
for example, consider a table like below (table name=accounts)
account owner_ids
PA account sa123,rt458,hf678,de348f
RA account gg678n,fk980,rt458,hf678
Here, I want to remove owner_ids "rt458" and "hf678"
from the entire table and after removing the commas should be perfect (I mean after removing also the remaining owner_ids should be separated by comma as before and there should not be any unnecessary comma in the column)
Any kind of help is appreciated, Help me out guys
mysql sql database oracle plsql
Helping you out here means pointing out that your CSV data is unnormalized, and therefore hard to work with. You should get owner id onto a separate row.
– Tim Biegeleisen
Nov 11 at 7:33
@vega et al. - I see you marked this as a duplicate of another question. What you did here is an abuse; the question you linked to is different. You may have your own opinion on how bad the OP's data model is (I agree with that opinion), but that doesn't give you the right to abuse the system. Shame on you.
– mathguy
Nov 11 at 14:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
for example, consider a table like below (table name=accounts)
account owner_ids
PA account sa123,rt458,hf678,de348f
RA account gg678n,fk980,rt458,hf678
Here, I want to remove owner_ids "rt458" and "hf678"
from the entire table and after removing the commas should be perfect (I mean after removing also the remaining owner_ids should be separated by comma as before and there should not be any unnecessary comma in the column)
Any kind of help is appreciated, Help me out guys
mysql sql database oracle plsql
for example, consider a table like below (table name=accounts)
account owner_ids
PA account sa123,rt458,hf678,de348f
RA account gg678n,fk980,rt458,hf678
Here, I want to remove owner_ids "rt458" and "hf678"
from the entire table and after removing the commas should be perfect (I mean after removing also the remaining owner_ids should be separated by comma as before and there should not be any unnecessary comma in the column)
Any kind of help is appreciated, Help me out guys
mysql sql database oracle plsql
mysql sql database oracle plsql
edited Nov 11 at 7:12
krithikaGopalakrisnan
535218
535218
asked Nov 11 at 7:05
David
91
91
Helping you out here means pointing out that your CSV data is unnormalized, and therefore hard to work with. You should get owner id onto a separate row.
– Tim Biegeleisen
Nov 11 at 7:33
@vega et al. - I see you marked this as a duplicate of another question. What you did here is an abuse; the question you linked to is different. You may have your own opinion on how bad the OP's data model is (I agree with that opinion), but that doesn't give you the right to abuse the system. Shame on you.
– mathguy
Nov 11 at 14:31
add a comment |
Helping you out here means pointing out that your CSV data is unnormalized, and therefore hard to work with. You should get owner id onto a separate row.
– Tim Biegeleisen
Nov 11 at 7:33
@vega et al. - I see you marked this as a duplicate of another question. What you did here is an abuse; the question you linked to is different. You may have your own opinion on how bad the OP's data model is (I agree with that opinion), but that doesn't give you the right to abuse the system. Shame on you.
– mathguy
Nov 11 at 14:31
Helping you out here means pointing out that your CSV data is unnormalized, and therefore hard to work with. You should get owner id onto a separate row.
– Tim Biegeleisen
Nov 11 at 7:33
Helping you out here means pointing out that your CSV data is unnormalized, and therefore hard to work with. You should get owner id onto a separate row.
– Tim Biegeleisen
Nov 11 at 7:33
@vega et al. - I see you marked this as a duplicate of another question. What you did here is an abuse; the question you linked to is different. You may have your own opinion on how bad the OP's data model is (I agree with that opinion), but that doesn't give you the right to abuse the system. Shame on you.
– mathguy
Nov 11 at 14:31
@vega et al. - I see you marked this as a duplicate of another question. What you did here is an abuse; the question you linked to is different. You may have your own opinion on how bad the OP's data model is (I agree with that opinion), but that doesn't give you the right to abuse the system. Shame on you.
– mathguy
Nov 11 at 14:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can use REPLACE function twice:
update accounts
set owner_ids = REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678');
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
Or you can use REGEXP_REPLACE to replace/remove those values by regex:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,rt458||,hf678)', '');
REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern.
To support case where rt458 is the first entry in the CSV list (As @TimBiegeleisen commented) you will have to allow no comma as:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,{0,1}rt458||,{0,1}hf678)', '');
Using replace you will need to double calls:
update accounts
set owner_ids = REPLACE(REPLACE(REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678', 'rt458,', 'hf678,');
Also you can see other solutions to remove value from comma separated string
1
What about the case wherert458
is the first entry in the CSV list?
– Tim Biegeleisen
Nov 11 at 7:32
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
What about the case where there is another token,rt4586
, which mustn't be removed? You will just removert458
from it, leaving the6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.
– mathguy
Nov 11 at 15:46
add a comment |
up vote
0
down vote
UPDATE accounts
SET owner_ids = TRIM(BOTH ',' FROM
REPLACE(REPLACE(CONCAT(',', owner_ids, ','), ',rt458,', ''), ',hf678,', '')
);
First prepend and append the field with a comma CONCAT(',', owner_ids, ',')
to make it uniform.
Then, remove ,rt458,
and ,hf678,
from it.
Later remove commas from both at the start and end which we added to make the structure uniform.
This will maintain single commas between the values.
Please note, you should prefer normalizing
owner_ids
so that such updates are easy to handle.
What if one of the tokens isrt4586
? Your solution will removert458
, leaving the6
behind (when in fact that token shouldn't be touched in the first place).
– mathguy
Nov 11 at 15:47
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use REPLACE function twice:
update accounts
set owner_ids = REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678');
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
Or you can use REGEXP_REPLACE to replace/remove those values by regex:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,rt458||,hf678)', '');
REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern.
To support case where rt458 is the first entry in the CSV list (As @TimBiegeleisen commented) you will have to allow no comma as:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,{0,1}rt458||,{0,1}hf678)', '');
Using replace you will need to double calls:
update accounts
set owner_ids = REPLACE(REPLACE(REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678', 'rt458,', 'hf678,');
Also you can see other solutions to remove value from comma separated string
1
What about the case wherert458
is the first entry in the CSV list?
– Tim Biegeleisen
Nov 11 at 7:32
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
What about the case where there is another token,rt4586
, which mustn't be removed? You will just removert458
from it, leaving the6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.
– mathguy
Nov 11 at 15:46
add a comment |
up vote
0
down vote
You can use REPLACE function twice:
update accounts
set owner_ids = REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678');
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
Or you can use REGEXP_REPLACE to replace/remove those values by regex:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,rt458||,hf678)', '');
REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern.
To support case where rt458 is the first entry in the CSV list (As @TimBiegeleisen commented) you will have to allow no comma as:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,{0,1}rt458||,{0,1}hf678)', '');
Using replace you will need to double calls:
update accounts
set owner_ids = REPLACE(REPLACE(REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678', 'rt458,', 'hf678,');
Also you can see other solutions to remove value from comma separated string
1
What about the case wherert458
is the first entry in the CSV list?
– Tim Biegeleisen
Nov 11 at 7:32
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
What about the case where there is another token,rt4586
, which mustn't be removed? You will just removert458
from it, leaving the6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.
– mathguy
Nov 11 at 15:46
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use REPLACE function twice:
update accounts
set owner_ids = REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678');
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
Or you can use REGEXP_REPLACE to replace/remove those values by regex:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,rt458||,hf678)', '');
REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern.
To support case where rt458 is the first entry in the CSV list (As @TimBiegeleisen commented) you will have to allow no comma as:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,{0,1}rt458||,{0,1}hf678)', '');
Using replace you will need to double calls:
update accounts
set owner_ids = REPLACE(REPLACE(REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678', 'rt458,', 'hf678,');
Also you can see other solutions to remove value from comma separated string
You can use REPLACE function twice:
update accounts
set owner_ids = REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678');
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
Or you can use REGEXP_REPLACE to replace/remove those values by regex:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,rt458||,hf678)', '');
REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern.
To support case where rt458 is the first entry in the CSV list (As @TimBiegeleisen commented) you will have to allow no comma as:
update accounts
set owner_ids = REGEXP_REPLACE(owner_ids,
'(,{0,1}rt458||,{0,1}hf678)', '');
Using replace you will need to double calls:
update accounts
set owner_ids = REPLACE(REPLACE(REPLACE(REPLACE(owner_ids,
',rt458' ),',hf678', 'rt458,', 'hf678,');
Also you can see other solutions to remove value from comma separated string
edited Nov 11 at 7:49
answered Nov 11 at 7:16
user7294900
18.7k93056
18.7k93056
1
What about the case wherert458
is the first entry in the CSV list?
– Tim Biegeleisen
Nov 11 at 7:32
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
What about the case where there is another token,rt4586
, which mustn't be removed? You will just removert458
from it, leaving the6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.
– mathguy
Nov 11 at 15:46
add a comment |
1
What about the case wherert458
is the first entry in the CSV list?
– Tim Biegeleisen
Nov 11 at 7:32
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
What about the case where there is another token,rt4586
, which mustn't be removed? You will just removert458
from it, leaving the6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.
– mathguy
Nov 11 at 15:46
1
1
What about the case where
rt458
is the first entry in the CSV list?– Tim Biegeleisen
Nov 11 at 7:32
What about the case where
rt458
is the first entry in the CSV list?– Tim Biegeleisen
Nov 11 at 7:32
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
@TimBiegeleisen Thanks for commenting, I updated my answer
– user7294900
Nov 11 at 7:45
What about the case where there is another token,
rt4586
, which mustn't be removed? You will just remove rt458
from it, leaving the 6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.– mathguy
Nov 11 at 15:46
What about the case where there is another token,
rt4586
, which mustn't be removed? You will just remove rt458
from it, leaving the 6
. The problem can be solved with REPLACE, but the solution must be written with much more care than you have.– mathguy
Nov 11 at 15:46
add a comment |
up vote
0
down vote
UPDATE accounts
SET owner_ids = TRIM(BOTH ',' FROM
REPLACE(REPLACE(CONCAT(',', owner_ids, ','), ',rt458,', ''), ',hf678,', '')
);
First prepend and append the field with a comma CONCAT(',', owner_ids, ',')
to make it uniform.
Then, remove ,rt458,
and ,hf678,
from it.
Later remove commas from both at the start and end which we added to make the structure uniform.
This will maintain single commas between the values.
Please note, you should prefer normalizing
owner_ids
so that such updates are easy to handle.
What if one of the tokens isrt4586
? Your solution will removert458
, leaving the6
behind (when in fact that token shouldn't be touched in the first place).
– mathguy
Nov 11 at 15:47
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
add a comment |
up vote
0
down vote
UPDATE accounts
SET owner_ids = TRIM(BOTH ',' FROM
REPLACE(REPLACE(CONCAT(',', owner_ids, ','), ',rt458,', ''), ',hf678,', '')
);
First prepend and append the field with a comma CONCAT(',', owner_ids, ',')
to make it uniform.
Then, remove ,rt458,
and ,hf678,
from it.
Later remove commas from both at the start and end which we added to make the structure uniform.
This will maintain single commas between the values.
Please note, you should prefer normalizing
owner_ids
so that such updates are easy to handle.
What if one of the tokens isrt4586
? Your solution will removert458
, leaving the6
behind (when in fact that token shouldn't be touched in the first place).
– mathguy
Nov 11 at 15:47
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
add a comment |
up vote
0
down vote
up vote
0
down vote
UPDATE accounts
SET owner_ids = TRIM(BOTH ',' FROM
REPLACE(REPLACE(CONCAT(',', owner_ids, ','), ',rt458,', ''), ',hf678,', '')
);
First prepend and append the field with a comma CONCAT(',', owner_ids, ',')
to make it uniform.
Then, remove ,rt458,
and ,hf678,
from it.
Later remove commas from both at the start and end which we added to make the structure uniform.
This will maintain single commas between the values.
Please note, you should prefer normalizing
owner_ids
so that such updates are easy to handle.
UPDATE accounts
SET owner_ids = TRIM(BOTH ',' FROM
REPLACE(REPLACE(CONCAT(',', owner_ids, ','), ',rt458,', ''), ',hf678,', '')
);
First prepend and append the field with a comma CONCAT(',', owner_ids, ',')
to make it uniform.
Then, remove ,rt458,
and ,hf678,
from it.
Later remove commas from both at the start and end which we added to make the structure uniform.
This will maintain single commas between the values.
Please note, you should prefer normalizing
owner_ids
so that such updates are easy to handle.
edited Nov 12 at 7:35
answered Nov 11 at 7:28
Samir
5,0832626
5,0832626
What if one of the tokens isrt4586
? Your solution will removert458
, leaving the6
behind (when in fact that token shouldn't be touched in the first place).
– mathguy
Nov 11 at 15:47
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
add a comment |
What if one of the tokens isrt4586
? Your solution will removert458
, leaving the6
behind (when in fact that token shouldn't be touched in the first place).
– mathguy
Nov 11 at 15:47
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
What if one of the tokens is
rt4586
? Your solution will remove rt458
, leaving the 6
behind (when in fact that token shouldn't be touched in the first place).– mathguy
Nov 11 at 15:47
What if one of the tokens is
rt4586
? Your solution will remove rt458
, leaving the 6
behind (when in fact that token shouldn't be touched in the first place).– mathguy
Nov 11 at 15:47
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
Oh yes, you're right. Updated my answer. Thank you for pointing it out.
– Samir
Nov 12 at 7:36
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246573%2fhow-to-selectively-remove-a-particular-value-from-a-column-which-contains-more-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Helping you out here means pointing out that your CSV data is unnormalized, and therefore hard to work with. You should get owner id onto a separate row.
– Tim Biegeleisen
Nov 11 at 7:33
@vega et al. - I see you marked this as a duplicate of another question. What you did here is an abuse; the question you linked to is different. You may have your own opinion on how bad the OP's data model is (I agree with that opinion), but that doesn't give you the right to abuse the system. Shame on you.
– mathguy
Nov 11 at 14:31