How to select multiple column names (not values) in a table when column value is specified-Oracle - oracle

I have a table which has around 30 columns.Out of 30 columns i need to retrieve around 25 column names where value is set to some specified value(say 1).
I am not able to find a way to do this.Will multiple if statement work as below
select if columnname1='1' then 'columnname1' else null
if columnname2='1' then 'columnname2' else null from table.
In case the value is not set to 1, i don't want to retrieve the column names.
The below query can give me the column names but i can't specify the value with below query
select DISTINCT COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='table_name'.

Does this work for you?
SELECT COLUMN_NAME
from ALL_TAB_COLUMNS
where TABLE_NAME='TABLE_NAME'
AND INSTR(column_name,'1') >0

Related

How to only select existing values from oracle?

I have a table with a massive number of columns. So many, that when I do SELECT * I can't even see any values because all the columns fill up the screen. I'd like to do something like this:
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND <THIS COLUMN> IS NOT NULL
Is this possible? Note: VALUE is not a column.
There are so many questions on SO that ask this same question, but they have some bizarre twist, and the actual question is not answered.
I've tried:
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND VALUE NOT NULL
*
Invalid relational operator
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND VALUE <> ''
*
'VALUE': invalid identifier
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND COLUMN NOT NULL
*
Missing Expression
Bonus Questions:
Is there any way to force Oracle to only show one output screen at a time?
Is there a variable to use in the WHERE clause that relates to the current column? Such as: WHERE this.column = '1', where it would check each column to match that expression?
Is there any way to get back your last command in Oracle? (I have to remote into a Linux box running Oracle - it's all command line - can't even copy/paste, so I have to type every command by hand, with a wonky connection, so it's taking an extremely long time to debug this stuff)
If you are trying to find all the non null column values for a particular record you could try an unpivot provided all the columns you are unpivoting have the same data type:
SELECT *
FROM (select * from my_table where name like '%unique value%')
UNPIVOT [include nulls] (col_value FOR col_name IN (col1, col2, ..., coln))
with the above code null values will be excluded unless you include the optional include nulls statement, also you will need to explicitly list each column you want unpivoted.
If they don't all have the same data type, you can use a variation that doesn't necessarily prune away all the null values:
select *
from (select * from my_table where name like '%unique value%')
unpivot ((str_val, num_val, date_val)
for col_name in ((cola, col1, date1)
,(colb, col2, date2)
,(colc, col3, date1)));
You can have a fairly large set of column groups, though here I'm showing just three, one for each major data type, with the IN list you need to have a column listed for each column in your column group, though you can reuse columns as shown by the date_val column where I've used date1 twice. As an alternative to reusing an existing column, you could use a dummy column with a null value:
select *
from (select t1.*, null dummy from my_table t1 where name like '%unique value%')
unpivot ((str_val, num_val, date_val)
for col_name in ((dummy, col1, date1)
,(colb, dummy, date2)
,(colc, col3, dummy)));
Have tried this?
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND value IS NOT NULL;
Oracle / PLSQL: IS NOT NULL Condition
For row number:
SELECT field1, field2, ROW_NUMBER() OVER (PARTITION BY unique_field) R WHERE R=1;
Usually in Linux consoles you can use arrow up&down to repeat the last sentence.

how to get column names from Hive table, if that column contains null values?

I tried with following queries, these queries giving only the count of null values, What i need was to do a null check and return the column name.
select count(*)-count(columnA), count(*)-count(columnB) from table;
select count(*) from table where columnA is null;
You can also do
SELECT
SUM(IF(columnA is NULL,1,0)) as canulls,
SUM(IF(columnB is NULL,1,0)) as cbnulls
FROM table;
This will also give you the number of null fields for each columns.

How to use a table of column names in a select statement

I have a table that I will add columns to in the future and don't want to update all my queries when they're added. I'm grabbing a set of column names using information_schema.COLUMNS and I want to use those names as the columns in a SELECT statement, but that's where I'm stuck:
select * into #Temp
from information_schema.COLUMNS
where column_name like '%search_string%' and table_name like 'searched_table'
select ...
So what's the syntax to use that table of column names after the SELECT, instead of listing each name individually?

Oracle - select all where one of the columns satisfies predicate

I have a table with 30+ rows and i want to do a select * however i also want to make sure that no rows containing null values are returned for a certain column. How can i do this without having to list each of the column names in the select?
My current SQL looks like this;
select *
from table
where (select column
from table
where column is not null) is > 20
However this does not work as the nested select returns more than one value.
Any suggestions please?
select * from table where column is not null
maybe?
SELECT *
FROM table
WHERE column > 20
Is that sufficient?

CHAR_USED query returned '0 rows fetched from 1 column'

I created the following table:
Create table temp.test(c1 VARCHAR2(10 BYTE));
I was trying to use CHAR_USED to determine whether the column size is in BYTES or CHARS but all I am getting back is '0 rows fetched from 1 column'. The database version i am using is Oracle 11g. Does anyone have a clue as to why it is not return the semantic length information for this table?
The query used are as follows:
select CHAR_USED from all_tab_columns where table_name='temp.test'
select CHAR_USED from all_tab_columns where table_name='test' and owner = 'temp'
Assuming that you are not using case-sensitive identifiers (which you are not and should not), object names are stored in the data dictionary in upper case. So when you query a table like all_tab_columns, you'd need to use upper-case
SELECT column_name, char_used
FROM all_tab_columns
WHERE table_name = 'TEST'
AND owner = 'TEMP'

Resources