문제

I have these entries in a field in one table under a DB running Postgres 9.4:

copy schema_name.table_name to '/path/path2/path3/path4/reports/export" 

and would like to replace ONLY the word "reports" by "reports_test"

What would be the proper way to do this?

I understand is a regular expression in an SQL update query but not exactly sure.

도움이 되었습니까?

해결책

This replacement can be done with:

UPDATE tableName SET fieldName =
     regexp_replace(fieldName, '\yreports\y', 'reports_test', 'g');

From the documentation:

\y matches only at the beginning or end of a word

The last argument 'g' is meant to replace all occurrences, otherwise it would replace only the first found.

You may also add a WHERE clause to limit the rows updated to those that match:

UPDATE tableName SET fieldName =
     regexp_replace(fieldName, '\yreports\y', 'reports_test', 'g')
WHERE fieldName ~ '\yreports\y';
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top