Greg Imo
Greg Imo

Reputation: 15

Select columns of a particular data type in PostgreSQL

I am looking for a piece of information, but the table I'm looking at has dozens of columns, and I can't remember the exact name of the column. I only know that it is of a date type. Is there a way to select only date columns so that it will be easier to find the name of the column?

e.g.

SELECT * FROM "MySchema"."MyTable"
WHERE {column.data_type} = 'date'

Upvotes: 1

Views: 111

Answers (1)

Francesco Riccio
Francesco Riccio

Reputation: 78

You can use the information_schema.columns view:

select column_name
from information_schema.columns
where table_schema = 'MySchema' and table_name = 'MyTable' and data_type = 'date';

Now that you have the names of the columns of type date, you can use that information create a view that selects only the values of such columns.

Upvotes: 2

Related Questions