Solved

SQL Query to replace values in columns

Posted on 2016-07-23
4
89 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

920 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

17 Experts available now in Live!

Get 1:1 Help Now