Remove a substring in a varchar field from multiple rows of a table

slashms

I would like to consult about the best way to remove a certain substring from a varchar field of every row in a table. Let's assume I have a single column table, the column names is "user_list" and it is a varchar field that contain user names seperated by ";". for example:
row1: james;david;moses
row2: mary;moses;terry
row3: ronaldo;messi;zlatan

the lists are not sorted in anyway. I want to crate a SP that gets a username and removes it from every row it appears, for instance if the db is the example above and i got as an input 'moses' I would like it to look like row1: james;david;
row2: mary;terry
row3: ronaldo;messi;zlatan

I want it to be a single update command and not a cursor, and i'm thinking with myself (and now with you) what is the best way to do it.

Thanks!

Gordon Linoff

You have a very poor data structure. SQL has this great structure for storing lists of things. It is called a "table". In particular, you want a junction table instead of storing values as lists.

That said, you cannot always control how data is structured. The following should help:

update table t
    set usernames = replace(replace(';' + usernames + ';', ';' + @UserName + ';', ''), ';;', ';')
    where ';' + usernames + ';' like '%;' + @UserName + ';%';

This will put a semicolon at the beginning and the end of the list. If that is a problem, you can remove them using left() or stuff().

EDIT:

To remove the ; at the beginning, use stuff():

update table t
    set usernames = stuff(replace(replace(';' + usernames + ';', ';' + @UserName + ';', ''), ';;', ';'), 1, 1, '')
    where ';' + usernames + ';' like '%;' + @UserName + ';%';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Field involving multiple rows from another table

From Dev

Selecting rows using multiple LIKE conditions from a table field

From Dev

SQL insert multiple rows from single field in another table

From Dev

Fetching Multiple Rows from Table and Adding Them into Another Table and then Remove from Previous Table, Using Checkboxes

From Dev

Get substring from varchar column in SQL server table

From Dev

remove rows from one table, where a field in a column matches that of same column in different table

From Dev

Concatenate field from multiple rows into single field

From Dev

SELECT * FROM table WHERE "substring" IN table_field?

From Dev

Deleting multiple rows from a table

From Dev

Deleting multiple rows from a table

From Dev

MySQL SELECT Substring of rows that do not exist from other TABLE

From Dev

SQL - Remove Duplicate Rows From Table

From Dev

SSRS Conditionally remove rows from a table

From Dev

Remove unnecessary rows from a relational table

From Dev

Remove unnecessary padding from table rows in CSS

From Dev

Jquery to remove all the rows from closest table

From Dev

Delete rows from Table, remove unpaid transaction

From Dev

Add & Remove Cells from Cloned Table Rows

From Dev

remove every other field from a table

From Dev

How to remove multiple selected rows from grid

From Dev

Remove a substring from String

From Dev

Improve performance of a join between a varchar field and a substring applied to another field

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

Insert multiple rows from select into another table

From Dev

Create multiple rows in table from array?

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

Inserting multiple rows to database table from a form

From Dev

Combine multiple rows in one from same table

From Dev

Inserting multiple rows to database table from a form

Related Related

  1. 1

    Field involving multiple rows from another table

  2. 2

    Selecting rows using multiple LIKE conditions from a table field

  3. 3

    SQL insert multiple rows from single field in another table

  4. 4

    Fetching Multiple Rows from Table and Adding Them into Another Table and then Remove from Previous Table, Using Checkboxes

  5. 5

    Get substring from varchar column in SQL server table

  6. 6

    remove rows from one table, where a field in a column matches that of same column in different table

  7. 7

    Concatenate field from multiple rows into single field

  8. 8

    SELECT * FROM table WHERE "substring" IN table_field?

  9. 9

    Deleting multiple rows from a table

  10. 10

    Deleting multiple rows from a table

  11. 11

    MySQL SELECT Substring of rows that do not exist from other TABLE

  12. 12

    SQL - Remove Duplicate Rows From Table

  13. 13

    SSRS Conditionally remove rows from a table

  14. 14

    Remove unnecessary rows from a relational table

  15. 15

    Remove unnecessary padding from table rows in CSS

  16. 16

    Jquery to remove all the rows from closest table

  17. 17

    Delete rows from Table, remove unpaid transaction

  18. 18

    Add & Remove Cells from Cloned Table Rows

  19. 19

    remove every other field from a table

  20. 20

    How to remove multiple selected rows from grid

  21. 21

    Remove a substring from String

  22. 22

    Improve performance of a join between a varchar field and a substring applied to another field

  23. 23

    Insert multiple rows into a MySQL database from a table

  24. 24

    Insert multiple rows from select into another table

  25. 25

    Create multiple rows in table from array?

  26. 26

    MYSQL - UPDATE multiple rows from another table

  27. 27

    Inserting multiple rows to database table from a form

  28. 28

    Combine multiple rows in one from same table

  29. 29

    Inserting multiple rows to database table from a form

HotTag

Archive