Solved

Change text in a table from val1 to val2 in MSSQL

Posted on 2014-01-30
11
285 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
  • 7
  • 3
11 Comments
 
LVL 31

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 65

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
 
LVL 31

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 31

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 31

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 31

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 31

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 31

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query Assistance 4 30
PL/SQL query 14 40
Query Missing Money orders... 6 67
SQL Split character from numbers 3 16
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now