Is it possible to append string to many matching rows in one query

Viewed 50

For an example, i have following table:

Id Name Color
1 FirstCar White
2 SecondCar Yellow
3 ThirdCar Red
4 FourthCar White
5 FifthCar Green
6 SixthCar Blue

What i need to achieve: append string "_123" to field "Name" for all rows where "Name" is "SecondCar", "FourthCar" or "SixthCar" resulting following:

Id Name Color
1 FirstCar White
2 SecondCar_123 Yellow
3 ThirdCar Red
4 FourthCar_123 White
5 FifthCar Green
6 SixthCar_123 Blue

How can i do this?

2 Answers

It's quite straightforward by using a CASE WHEN statement together with a CONCAT function:

SELECT
  id,
  CASE
    WHEN name IN ("SecondCar", "FourthCar", "SixthCar")
      THEN CONCAT(name, "_123")
    ELSE name
  END AS name,
  color
FROM your_table;
Related