Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
Executing a Function on all Columns in a Select Query

I have defined this function, to trim trailing whitespace:

create or replace function trim_trailing_whitespace(value text) returns text as $$ begin return regexp_replace(value, '\s+$', ''); end; $$ language plpgsql immutable;

It works correctly when used in queries like this one:

select trim_trailing_whitespace(SomeColumn), count(*) from MyTable group by SomeColumn;

However, it fails when I try to utilize it with a wildcard, like so:

select trim_trailing_whitespace(*) from MyTable;

LINE 1: select trim_trailing_whitespace(*) from MyTable;

HINT: No function matches the given name and argument types. You might need to add explicit type casts.

How can I execute a function on all columns within a select query? In my case, I want to trim the trailing whitespace off of each column when performing the selection.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If possible, define a second VARIADIC version of your function. Iterate the args with a FOREACH and execute your current function on each argument.

Good example here: https://www.depesz.com/2008/07/31/waiting-for-84-variadic-functions/

over 4 years ago · Santiago Trujillo Report

0

You can use dynamic sql or expand * yourself manually.

SELECT FORMAT(
  'SELECT %s FROM %I.%I.%I;',
  string_agg(
    FORMAT(
      'trim_trailing_whitespace(%I)',
      column_name
    ),
    ', '
  )
  , table_catalog, table_schema, table_name
)
FROM information_schema.columns
WHERE table_catalog = current_catalog
  AND table_schema = current_schema
  AND table_name = 'MyTable'
GROUP BY table_catalog, table_schema, table_name;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!