Solved

SQL Query to replace values in columns

Posted on 2016-07-23
4
96 Views
Last Modified: 2016-07-25
Hi, I would like to know how to write a query that would search all the columns and replace the names.

Example per screenshot, I would like to do the following below. I am not sure if it is ideal to create an Index called, "Replacement" and based on the column that matches to an entry from the Index, then do certain replacement. Thanks!

* Search Category 1 - Category 6 columns
* If "Furniture > Consoles" then replace with "Consoles"
* If "Sale > All Sale > Furniture" then replace with "Sale"
* If "More Themes > French Decor > French Furniture" then replace with  "Theme > French Furniture"



SQL6.jpg
0
Comment
Question by:Member_2_7967487
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 41725749
You have to do it column by column, value by value:

UPDATE <YourTable> SET  <Column1> = 'Consoles' WHERE <Column1> = 'Furniture > Consoles'
UPDATE <YourTable> SET  <Column2> = 'Consoles' WHERE <Column2> = 'Furniture > Consoles'
UPDATE <YourTable> SET  <Column3> = 'Consoles' WHERE <Column3> = 'Furniture > Consoles'
etc.

You have to provide correct table and column names instead of <name> into above command.

Of course, you may create a script/stored procedure which could build the UPDATE commands dynamically for all columns you are intending to update but that's more work...

Don't forget to backup your database as the first step!
0
 
LVL 24

Expert Comment

by:chaau
ID: 41726922
Just to correct the previous expert. You can do this using a single query, if required. The query in this case will use a CASE statement for each of the columns, like this:

UPDATE tbl1 SET  Column1 = 
CASE WHEN Column1 = 'Furniture > Consoles' THEN 'Consoles' 
     WHEN Column1 = 'Sale > All Sale > Furniture' THEN 'Sale'
     WHEN Column1 = 'More Themes > French Decor > French Furniture' THEN 'Theme > French Furniture'
     ELSE Column1 END,
Column2 = 
CASE WHEN Column2 = 'Furniture > Consoles' THEN 'Consoles' 
     WHEN Column2 = 'Sale > All Sale > Furniture' THEN 'Sale'
     WHEN Column2 = 'More Themes > French Decor > French Furniture' THEN 'Theme > French Furniture'
     ELSE Column2 END,
Column3 = 
CASE WHEN Column3 = 'Furniture > Consoles' THEN 'Consoles' 
     WHEN Column3 = 'Sale > All Sale > Furniture' THEN 'Sale'
     WHEN Column3 = 'More Themes > French Decor > French Furniture' THEN 'Theme > French Furniture'
     ELSE Column3 END
-- etc. Continue for other columns

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41727459
a tiny "correction": a single query approach would use "case expressions"

"case statements" are branching mechanisms usually associated with stored procedures or functions (yes I know; I'm being "pedantic")

but also note:
the term"expression" applies to something that evaluates to a single value, hence "case expression"
0
 
LVL 24

Expert Comment

by:chaau
ID: 41728492
Thanks Paul
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

815 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

10 Experts available now in Live!

Get 1:1 Help Now