1

How to select all the columns whose names begins with "img" from a PostgreSQL table?

I wrote that, but doesn't work

select 'img%' from mytable
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
ilFonta
  • 131
  • 1
  • 5

1 Answers1

2

You can use INFORMATION_SCHEMA views for this purpose:

create table tbl 
(
    id serial,
    img1 text,
    img2 text,
    foo int,
    bar int,
    img14 text
);
select table_catalog, table_name, column_name
from   information_schema.columns
where  table_catalog = 'postgres'
and    table_name = 'tbl'
and    column_name like 'img%';
table_catalog | table_name | column_name
:------------ | :--------- | :----------
postgres      | tbl        | img1       
postgres      | tbl        | img2       
postgres      | tbl        | img14      

db<>fiddle here

McNets
  • 23,979
  • 11
  • 51
  • 89