Solved

SQL Query to replace values in columns

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Read about achieving the basic levels of HRIS security in the workplace.
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

19 Experts available now in Live!

Get 1:1 Help Now