Select all columns except null or NVL all null

 ̄綄美尐妖づ 提交于 2019-12-11 09:44:42

问题


Is any way to select all columns in a record where fields are not null or NVL the null fields to ''? Im working on ORACLE

Something like this

SELECT * IS NOT NULL
FROM table
WHERE id_table ='001'

or like this

SELECT NVL(*,'')
FROM table
WHERE id_table ='001'

回答1:


Just reverse the NULL logic in the demonstration about Find all columns having at least a NULL value from all tables in the schema.

For example,

FIND_NULL_COL is a simple user defined function(UDF) which will return 1 for the column which has at least one NULL value :

SQL> CREATE OR REPLACE
  2    FUNCTION FIND_NULL_COL(
  3        TABLE_NAME  VARCHAR2,
  4        COLUMN_NAME VARCHAR2)
  5      RETURN NUMBER
  6    IS
  7      cnt NUMBER;
  8    BEGIN
  9      CNT :=1;
 10      EXECUTE IMMEDIATE 'select count(1) from '
 11                        ||TABLE_NAME||' where ' ||COLUMN_NAME||' is not null
 12                        and deptno = 20' INTO cnt;
 13      RETURN
 14      CASE
 15      WHEN CNT=0 THEN
 16        1
 17      ELSE
 18        0
 19      END;
 20    END;
 21    /

Function created.

Call the function in SQL to get the NULL status of all the column of any table :

SQL>   SET pagesize 1000
SQL>   column owner format A10;
SQL>   column column_name format A20;
SQL>   COLUMN TABLE_NAME FORMAT A20;
SQL>   column n format A1;
SQL>   SELECT c.OWNER,
  2      c.TABLE_NAME,
  3      c.COLUMN_NAME,
  4      C.NULLABLE,
  5      FIND_NULL_COL(c.TABLE_NAME,c.COLUMN_NAME) null_status
  6    FROM all_tab_columns c
  7    WHERE C.OWNER    =USER
  8    AND c.TABLE_NAME = 'EMP'
  9    ORDER BY C.OWNER,
 10      C.TABLE_NAME,
 11      C.COLUMN_ID
 12  /

OWNER      TABLE_NAME           COLUMN_NAME          N NULL_STATUS
---------- -------------------- -------------------- - -----------
SCOTT      EMP                  EMPNO                N           0
SCOTT      EMP                  ENAME                Y           0
SCOTT      EMP                  JOB                  Y           0
SCOTT      EMP                  MGR                  Y           0
SCOTT      EMP                  HIREDATE             Y           0
SCOTT      EMP                  SAL                  Y           0
SCOTT      EMP                  COMM                 Y           1
SCOTT      EMP                  DEPTNO               Y           0

8 rows selected.

SQL>

So, NULL_STATUS "1" is the column which has NULL value(s).



来源:https://stackoverflow.com/questions/26064254/select-all-columns-except-null-or-nvl-all-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!