Solved

Change text in a table from val1 to val2 in MSSQL

Posted on 2014-01-30
11
301 Views
Last Modified: 2014-03-03
How would you write a query that would change all occurances in a table of val1 to val2?
0
Comment
Question by:hrolsons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
11 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39822000
Regardless of the column in which those values exist?
0
 

Author Comment

by:hrolsons
ID: 39822043
Correct, in all columns.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39822054
<air code>

UPDATE YourTable
SET
   Column1 = CASE Column1 WHEN 'val1' THEN 'val2' END,
   Column2 = CASE Column2 WHEN 'val1' THEN 'val2' END,
   Column3 = CASE Column3 WHEN 'val1' THEN 'val2' END,
   ColumnLast = CASE ColumnLast WHEN 'val1' THEN 'val2' END

Of course, this will only work for those columns that have the same data type as whatever 'val1' and 'val2' is.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 32

Expert Comment

by:awking00
ID: 39823981
Not sure I have the syntax right, but perhaps something like this -
declare @sql varchar(max)
declare @colname varchar(100)
declare col_cursor for
select column_name
from information_schema.columns
where table_name = 'yourtable'
and data_type = <whatever val1 datatype is>

open col_cursor
fetch next from col_cursor into @colname
while @@fetch_status = 0
begin
@sql = 'update yourtable set ' + @colname + ' = replace(' + @colname + ',val1, val2)'
exec @sql
end
close col_cursor
0
 

Author Comment

by:hrolsons
ID: 39824726
And, the cell can contain other info.  For instance:

"I love val1 and his brother"

Would change to:

"I love val2 and his brother"
0
 
LVL 32

Expert Comment

by:awking00
ID: 39824880
Don't have sql server handy but the same thing's true in most DBMS -
SQL> select replace('I love val1 and his brother','val1','val2') from dual;

REPLACE('ILOVEVAL1ANDHISBRO
---------------------------
I love val2 and his brother
0
 
LVL 32

Expert Comment

by:awking00
ID: 39824892
Note - if val1 and val2 are strings, the dynamic sql will need to include the extra quotes -
sql = '... replace(' + @colname + ',' ' val1' '.' 'val2' ')'
0
 
LVL 32

Expert Comment

by:awking00
ID: 39824896
Before doing the execution of a dynamic update statement, I would print the sql statement first to insure that the syntax is correct.
0
 

Author Comment

by:hrolsons
ID: 39828136
So the "Select" is working.  How do I commit the changes to the table?
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 39829393
@sql = 'update yourtable set ' + @colname + ' = replace(' + @colname + ',val1, val2)'
begin transaction x
exec @sql
commit transaction x
0
 
LVL 32

Expert Comment

by:awking00
ID: 39829396
I'm not sure but execute statement may require parentheses -
exec(@sql)
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question