Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Change text in a table from val1 to val2 in MSSQL

Posted on 2014-01-30
11
Medium Priority
?
305 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

609 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