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!
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%'
Related
I am using the sql command SHOW COLUMNS in this way
SHOW COLUMNS FROM TABLE A;
It outputs multiple columns, specifically one called 'COLUMN_NAME' that I would like to select.
I tried doing
SELECT COLUMN_NAME FROM (SHOW COLUMNS FROM TABLE A);
which gives an error, is there another way I can just show one of the columns of the output? Is this because the output is not a table so I cannot query from it?
You can use the like function:
show columns like 'COLUMN_NAME' in table A;
There are limitations on using the output of the show command, so an alternate approach is to use the INFORMATION_SCHEMA.
select * from INFORMATION_SCHEMA.COLUMNS where
TABLE_SCHEMA = 'MY_SCHEMA'
and TABLE_NAME = 'A'
and COLUMN_NAME = 'COLUMN_NAME';
If you need to query the results of a show command, you can use the result_scan table function:
show columns like '%' in table t1;
select * from table(result_scan(last_query_id())) where "column_name" = 'COLUMN_NAME';
Remember to double quote the column names produced by the show command. It's a metadata result and the column names are lower case.
If I have a table with column names like that:
name,
name_raw,
item,
item_raw,
message,
message_raw
...
... etc.
How can I select every column that does not end with _raw dynamically?
Is something like this:
SELECT
[SOME REGEX LOGIC HERE]
FROM
mytable
or similar possible?
If you have so many columns that you cannot write a (static) query, you probably have to change the schema of your db.
If you cannot change the schema, here's a quick and dirty solution that utilizes postgresql's JSON capabilities. It does not return a traditional table with multiple columns, instead it returns a single column that contains json objects which contain all the columns from the original table whose name ends with _raw.
SELECT (
SELECT json_object_agg(key,value)
FROM json_each(to_json(t))
WHERE key ~ 'raw$'
) FROM mytable;
The idea is convert every row from mytable to a JSON object and then use JSON functions to filter the members of the object.
Use the information schema e.g.
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'my_schema'
AND table_name = 'mytable'
AND NOT column_name LIKE '%\_raw'
note because the underscore itself is a query wildcard it needs to be "escaped"
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Submenu';
The above query gave all column names in Submenu table but I want only three column names of Submenu table.
Is there any way to get column names?
I assumed this is SQL Server, so the below queries might not work on other RDBMS's).
If your question is how to find only the column names you need (presuming you know in advance which ones those are), you would have to do something like this:
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
AND COLUMN_NAME IN ('Column1', 'Column2', 'Column3')
At this moment, you basically are requesting a list of any column within your table, without any restrictions whatsoever.
Alternatively, if you're looking only for the first three column names, this would work:
SELECT TOP 3
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
ORDER BY
ORDERINAL_POSITION
In the latter case, you will have to determine how you want to sort the column names though (either by using something like ORDER BY COLUMN_NAME, in case you want them listed alphabetically, or ORDER BY ORDERINAL_POSITION in case you're trying to get them in the order they appear in the table).
If this is not what you meant, please elaborate on what you are trying to achieve.
SELECT TOP 3 COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Submenu';
As easy as that!
I have a large table with over 50 columns. And I would like to search those columns for a particular word. How do I do this in Oracle ?
thanks
You can use the following:
select COLUMN_NAME from ALL_TAB_COLUMNS
where TABLE_NAME='your_table_name' and COLUMN_NAME like '%whatever_word_required%';
Explanation: ALL_TAB_COLUMNS describes the columns of the tables, views, and clusters accessible to the current user. So from all the tables, since you want to search in a particular table so give your table-name in the query and specify the criteria for columns using the LIKE.
Say if your table name is "Table1" and column-name you are trying to search is "Employee", so the query becomes:
select COLUMN_NAME from ALL_TAB_COLUMNS
where TABLE_NAME='Table1' and COLUMN_NAME like '%Employee%';
For reference see the Oracle Docs
I need to know the column names of my table exchangecondition that contains "report" in their names. I tried to use USER _TAB_COLUMNS but that didn't work, because I get no rows selected with this query:
select * from USER_TAB_COLUMNS;
However, if I could get rows when I use USER_TAB_COLUMNS, I can use this query:
SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = 'exchangecondition' and column_name like '%report%'.
Can someone help me please?
Instead of using user_tab_columns, use all_tab_columns, provide table name and column name in upper case, provide owner name (incase you know it already).