0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.