Michael
Michael

Reputation: 1381

Select columns containing string

Is it possible to retrieve all data in columns containing a particular string? I do not have any control over the data structure and I rather shorten my code than to type everything if possible. As far as I know statements as LIKE and CONTAIN are only used to retrieve particular rows instead of columns. Lets say I have a table with column names: time, run1, run2, run3, ....., run 34, I would like to use something like:

SELECT columns FROM [Tablename] WHERE columns CONTAIN 'run';

Is this possible?

Thanks!

Upvotes: 2

Views: 13664

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

Use like.

In Oracle, you could do

SELECT column_name 
from all_tab_columns 
where table_name = 'YOUR_TABLE'
and lower(column_name) like 'run%'

In SQL Server, you could do

SELECT column_name 
from information_schema.columns
where table_name = 'YOUR_TABLE_NAME'
and lower(column_name) like 'run%'

Upvotes: 4

Related Questions