Home > Software design >  How to update column values to wrap everything in brackets in SQL?
How to update column values to wrap everything in brackets in SQL?

Time:12-11

I am trying to wrap column values in brackets, so that e.g. foo becomes [foo]. I tried:

UPDATE notes 
SET topics = regexp_replace(topics, '(.)', '[$1]')

However that does not error but also does not change the column values.

Any ideas how to do it?

CodePudding user response:

We don't need a regex to put a string in brackets.

In most DBMS (MYSQL, Postgres, SQLServer, Maria DB) this can be done with CONCAT:

UPDATE notes 
SET topics = CONCAT('[', topics, ']');

In a Oracle DB or SQLite, using || will do the same:

UPDATE notes 
SET topics = '[' || topics || ']';
  • Related