UPDATE and REPLACE part of a string

前端 未结 10 2302
渐次进展
渐次进展 2020-12-04 04:22

I\'ve got a table with two columns, ID and Value. I want to change a part of some strings in the second column.

Example of Table:



        
相关标签:
10条回答
  • 2020-12-04 05:00

    For anyone want to replace your script.

    update dbo.[TABLE_NAME] set COLUMN_NAME= replace(COLUMN_NAME, 'old_value', 'new_value') where COLUMN_NAME like %CONDITION%

    0 讨论(0)
  • 2020-12-04 05:06

    you should use the below update query

    UPDATE dbo.xxx SET Value=REPLACE(Value,'123\','') WHERE Id IN(1, 2, 3, 4)
    
    UPDATE dbo.xxx SET Value=REPLACE(Value,'123\','') WHERE Id <= 4
    

    Either of the above queries should work.

    0 讨论(0)
  • 2020-12-04 05:14

    To make the query run faster in big tables where not every line needs to be updated, you can also choose to only update rows that will be modified:

    UPDATE dbo.xxx
    SET Value = REPLACE(Value, '123', '')
    WHERE ID <= 4
    AND Value LIKE '%123%'
    
    0 讨论(0)
  • 2020-12-04 05:18

    You don't need wildcards in the REPLACE - it just finds the string you enter for the second argument, so the following should work:

    UPDATE dbo.xxx
    SET Value = REPLACE(Value, '123\', '')
    WHERE ID <=4
    

    (I also added the \ in the replace as I assume you don't need that either)

    0 讨论(0)
  • 2020-12-04 05:20
    UPDATE table_name
    SET field_name = '0'
    WHERE field_name IS Null
    
    0 讨论(0)
  • 2020-12-04 05:23

    Try to remove % chars as below

    UPDATE dbo.xxx
    SET Value = REPLACE(Value, '123', '')
    WHERE ID <=4
    
    0 讨论(0)
提交回复
热议问题