I have a table in a postgres database with data that looks like this:
XX-001111111-11-Q011111o_ABC_90001u-0000101-9
The table has 1000 rows with similar looking strings.
I want to update every row to add the ‘AB-‘ prefix to it.
So XX-001111111-11-Q011111o_ABC_90001u-0000101-9 would look like AB-XX-001111111-11-Q011111o_ABC_90001u-0000101-9
I assume I can do this with regex_replace but everything I’ve tried has not worked.
3
Answers
Assume:
Test1
sname
SQL:
I tested it in my environment and it can be executed successfully.
PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
You don’t need a regex operation to change the value (prefix a string) of a column. An example using
psql
shell:The
concat
is a PostgreSQL string function. You can also use the||
(concatenation operator) to produce the same result:Refrerence: PostgreSQL String Functions and Operators
You can use
CONCAT
which enhances readability and clearly conveys the intent of the operation:here you go , an example: