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
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.Sign up to join this community
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