whytheq
whytheq

Reputation: 35607

UPDATING a section of string for column

We have the following:

create table #a_table 
(names char(10))
insert into #a_table
values
('A;B;C;'),
('B;C;D;'),
('A;B;C;E;'),
('A;C;'),
('A;B;'),
('A;'),
('A;C;E;');

How do we update the table and change every instance of C; with X;Y;

So
A;B;C; would become A;B;X;Y;
A;C;E; would become A;X;Y;E;
etc.


EDIT

A standard ANSI approach that is compatible on SQL-server would be prefereable.

Upvotes: 1

Views: 84

Answers (1)

Liath
Liath

Reputation: 10191

This all depends on which database you're actually using. In MSSQL Server You can use the SQL replace function

update #a_table set names = replace(names, 'C;', 'X;Y;')

Other databases have similar functions (such as the MySQL one here)

Upvotes: 1

Related Questions