简体   繁体   English

有没有办法在SELECT * FROM语句中TRIM所有数据?

[英]Is there a way to TRIM all data in a SELECT * FROM statement?

I am trying to select and trim all the entries from a table using the following statement: 我试图使用以下语句选择并修剪表中的所有条目:

SELECT TRIM(*) FROM TABLE

But I get an error. 但是我收到了一个错误。 Is there a way to return all entries selected so they are trimmed for blank characters at the beginning and end of each string? 有没有办法返回所有选中的条目,以便在每个字符串的开头和结尾处为空白字符修剪它们?

You need to specify each string column by hand: 您需要手动指定每个字符串列:

SELECT TRIM(col1),       --LTRIM(RTRIM(...)) If RDBMS is SQL Server
       TRIM(col2),
       TRIM(col3),
       TRIM(col4)
      -- ...
FROM table

There is another problem with your proposal. 您的提案还有另一个问题。 * is placeholder for each column in table so there will be problem with trimming date/decimal/spatial data ... . *是表格中每列的占位符,因此修剪date/decimal/spatial data ...会出现问题date/decimal/spatial data ...

Addendum 附录

Using Oracle 18c Polymorphic Table Functions (provided code is just PoC, there is a space for a lot of improvements): 使用Oracle 18c Polymorphic Table Functions (提供的代码只是PoC,有很多改进的空间):

CREATE TABLE tab(id INT, d DATE,
                 v1 VARCHAR2(100), v2 VARCHAR2(100), v3 VARCHAR2(100) );

INSERT INTO tab(id, d,v1, v2, v3) 
VALUES (1, SYSDATE, '  aaaa   ', '     b   ', '  c');
INSERT INTO tab(id, d,v1, v2, v3)
VALUES (2, SYSDATE+1, '   afasd', '    ', '  d');
COMMIT;

SELECT * FROM tab;
-- Output
.----.-----------.-----------.-----------.-----.
| ID |     D     |    V1     |    V2     | V3  |
:----+-----------+-----------+-----------+-----:
|  1 | 02-MAR-18 |   aaaa    |      b    |   c |
:----+-----------+-----------+-----------+-----:
|  2 | 03-MAR-18 |     afasd |           |   d |
'----'-----------'-----------'-----------'-----'

And table function: 和表功能:

CREATE OR REPLACE PACKAGE ptf AS
  FUNCTION describe(tab IN OUT dbms_tf.table_t)RETURN dbms_tf.describe_t;      
  PROCEDURE FETCH_ROWS;
END ptf; 
/
CREATE OR REPLACE PACKAGE BODY  ptf AS
  FUNCTION describe(tab IN OUT dbms_tf.table_t) RETURN dbms_tf.describe_t AS
  new_cols DBMS_TF.COLUMNS_NEW_T;
  BEGIN
    FOR i IN 1 .. tab.column.count LOOP
        IF tab.column(i).description.type IN ( dbms_tf.type_varchar2) THEN
           tab.column(i).pass_through:=FALSE;
           tab.column(i).for_read:= TRUE;
           NEW_COLS(i) :=
              DBMS_TF.COLUMN_METADATA_T(name=> tab.column(i).description.name,
                                       type => tab.column(i).description.type);
        END IF;
    END LOOP;
    RETURN DBMS_TF.describe_t(new_columns=>new_cols, row_replication=>true);
    END;

  PROCEDURE FETCH_ROWS AS
    inp_rs DBMS_TF.row_set_t;
    out_rs DBMS_TF.row_set_t;
    rows   PLS_INTEGER;
  BEGIN 
    DBMS_TF.get_row_set(inp_rs, rows);
    FOR c IN 1 .. inp_rs.count() LOOP
      FOR r IN 1 .. rows LOOP
          out_rs(c).tab_varchar2(r) := TRIM(inp_rs(c).tab_varchar2(r));
      END LOOP;
    END LOOP;
    DBMS_TF.put_row_set(out_rs, replication_factor => 1);
  END;
END ptf; 

And final call: 最后的电话:

CREATE OR REPLACE FUNCTION trim_col(tab TABLE)
RETURN TABLE pipelined row polymorphic USING ptf;

SELECT *
FROM trim_col(tab);    -- passing table as table function argument

.----.-----------.-------.-----.----.
| ID |     D     |  V1   | V2  | V3 |
:----+-----------+-------+-----+----:
|  1 | 02-MAR-18 | aaaa  | b   | c  |
:----+-----------+-------+-----+----:
|  2 | 03-MAR-18 | afasd |  -  | d  |
'----'-----------'-------'-----'----'

db<>fiddle demo db <>小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM