I’m writing a SQL query in SQL Server in which I need to replace multiple string values with a single string value. For example
Product Quantity ------- -------- Apple 2 Orange 3 Banana 1 Vegetable 7 Dairy 6
would become
Product Quantity ------- -------- Fruit 2 Fruit 3 Fruit 1 Vegetable 7 Dairy 6
The only way I know how to do this is to use a nested REPLACE in the SELECT clause.
SELECT REPLACE('Banana', REPLACE('Orange', REPLACE('Banana', Product, 'Fruit'), 'Fruit'), 'Fruit') AS Product FROM Table
Is there an easier way?
EDIT: There may be other values in the Product category. See edited example above.
BradC has the best answer so far, but in case you are for some reason unable to create the additional table I wanted to post an adaption of Kibbee’s answer: