Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

trim

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?

like image 921
Stacey Avatar asked Dec 21 '15 08:12

Stacey


People also ask

How do I trim a select query?

The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do I cut all values in a column in SQL?

MS SQL does not have a trim function. You'll need to use rTrim and lTrim together. Show activity on this post.

How use TRIM in SQL JOIN?

trim() removes spaces on both ends of the string. If you want to remove only leading spaces, you can do: trim(leading ' ' from z. code1) . Note that I used more meaningful table aliases, in order to make the query easier to write and read.


1 Answers

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 ....

Addendum

Using Oracle 18c Polymorphic Table Functions(provided code is just PoC, there is a space for a lot of improvements):

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

like image 135
Lukasz Szozda Avatar answered Sep 21 '22 16:09

Lukasz Szozda