-1

How can I add brackets to each word of a string which contains multiple words in SQL? For example, I have:

string example = 'word1,word2,word3,word4,word5'

How can I convert it to something like this:

string example = '[word1],[word2],[word3],[word4],[word5]' 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Fatman123
  • 23
  • 6

1 Answers1

3

We can use a combination of REPLACE() and string concatenation:

SELECT val, CONCAT('[', REPLACE(val, ',', '],[') , ']') AS output
FROM yourTable;

On MySQL 8+, we can use a regex replacement:

SELECT val, REGEXP_REPLACE(val, '([^,]+)', '[$1]') AS output
FROM yourTable;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360