I have a dynamic table with an indertermined number of columns [statement_nn] with strings. I need to replace string parts by the correspondent item
The table looks like this: Original table
How could I do the operation having in mind that “statement” columns are variable and cannot specifically use them in a standard REPLACE statement?
This is the end result Im looking to get: Desired result
I tried to make an array of "statement" columns and replace items there, but I need to be able to keep column names to get the desired result
You can use EXECUTE IMMEDIATE
to dynamically select and replace your desired items. The way I solved it was replacing each placeholder manually with the following query:
execute immediate (
select 'select * replace(' ||
string_agg('regexp_replace(' ||
'regexp_replace(' ||
'regexp_replace(' || column_name || ',' || ' r"<name>", name)'
|| ',' || ' r"<surname>", surname)'
|| ',' || ' r"<registration_year>", CAST(registration_year as STRING))'
|| ' as ' || column_name, ', ') ||
') from project_name.dataset_name.table_name'
from `project_name.dataset_name.INFORMATION_SCHEMA.COLUMNS`
where table_name = 'table_name'
and STARTS_WITH(column_name, "statement_")
)
Remember to replace project_name
, dataset_name
, and table_name
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.