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












10















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?










share|improve this question




















  • 4





    The answer is no. But why do you store leading/trailing blanks?

    – jarlh
    Dec 21 '15 at 8:48








  • 7





    And adding another thought: SELECT * is bad. You can never trust in whether the colums will be the same over time so the format of your output will change and your application might get broken. Use explicit list of columns for SELECT instead.

    – frlan
    Dec 21 '15 at 8:51






  • 1





    stackoverflow.com/questions/507869/trim-all-database-fields check this link

    – Ubiquitous Developers
    Dec 21 '15 at 8:52











  • @jarlh Using PTF makes it possible.

    – Lukasz Szozda
    Mar 2 '18 at 17:39


















10















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?










share|improve this question




















  • 4





    The answer is no. But why do you store leading/trailing blanks?

    – jarlh
    Dec 21 '15 at 8:48








  • 7





    And adding another thought: SELECT * is bad. You can never trust in whether the colums will be the same over time so the format of your output will change and your application might get broken. Use explicit list of columns for SELECT instead.

    – frlan
    Dec 21 '15 at 8:51






  • 1





    stackoverflow.com/questions/507869/trim-all-database-fields check this link

    – Ubiquitous Developers
    Dec 21 '15 at 8:52











  • @jarlh Using PTF makes it possible.

    – Lukasz Szozda
    Mar 2 '18 at 17:39
















10












10








10


2






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?










share|improve this question
















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?







sql trim






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 2 '18 at 17:23









Lukasz Szozda

79.1k1061105




79.1k1061105










asked Dec 21 '15 at 8:46









StaceyStacey

1,06651937




1,06651937








  • 4





    The answer is no. But why do you store leading/trailing blanks?

    – jarlh
    Dec 21 '15 at 8:48








  • 7





    And adding another thought: SELECT * is bad. You can never trust in whether the colums will be the same over time so the format of your output will change and your application might get broken. Use explicit list of columns for SELECT instead.

    – frlan
    Dec 21 '15 at 8:51






  • 1





    stackoverflow.com/questions/507869/trim-all-database-fields check this link

    – Ubiquitous Developers
    Dec 21 '15 at 8:52











  • @jarlh Using PTF makes it possible.

    – Lukasz Szozda
    Mar 2 '18 at 17:39
















  • 4





    The answer is no. But why do you store leading/trailing blanks?

    – jarlh
    Dec 21 '15 at 8:48








  • 7





    And adding another thought: SELECT * is bad. You can never trust in whether the colums will be the same over time so the format of your output will change and your application might get broken. Use explicit list of columns for SELECT instead.

    – frlan
    Dec 21 '15 at 8:51






  • 1





    stackoverflow.com/questions/507869/trim-all-database-fields check this link

    – Ubiquitous Developers
    Dec 21 '15 at 8:52











  • @jarlh Using PTF makes it possible.

    – Lukasz Szozda
    Mar 2 '18 at 17:39










4




4





The answer is no. But why do you store leading/trailing blanks?

– jarlh
Dec 21 '15 at 8:48







The answer is no. But why do you store leading/trailing blanks?

– jarlh
Dec 21 '15 at 8:48






7




7





And adding another thought: SELECT * is bad. You can never trust in whether the colums will be the same over time so the format of your output will change and your application might get broken. Use explicit list of columns for SELECT instead.

– frlan
Dec 21 '15 at 8:51





And adding another thought: SELECT * is bad. You can never trust in whether the colums will be the same over time so the format of your output will change and your application might get broken. Use explicit list of columns for SELECT instead.

– frlan
Dec 21 '15 at 8:51




1




1





stackoverflow.com/questions/507869/trim-all-database-fields check this link

– Ubiquitous Developers
Dec 21 '15 at 8:52





stackoverflow.com/questions/507869/trim-all-database-fields check this link

– Ubiquitous Developers
Dec 21 '15 at 8:52













@jarlh Using PTF makes it possible.

– Lukasz Szozda
Mar 2 '18 at 17:39







@jarlh Using PTF makes it possible.

– Lukasz Szozda
Mar 2 '18 at 17:39














1 Answer
1






active

oldest

votes


















11














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





share|improve this answer





















  • 2





    Why downvoting? The answer is correct.

    – jarlh
    Dec 21 '15 at 8:55











  • Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

    – Lukasz Szozda
    Dec 21 '15 at 8:55








  • 1





    One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

    – Lukasz Szozda
    Dec 21 '15 at 9:02








  • 2





    It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

    – Andrew Morton
    Dec 21 '15 at 9:09











  • @AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

    – Lukasz Szozda
    Dec 21 '15 at 9:10













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34391793%2fis-there-a-way-to-trim-all-data-in-a-select-from-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














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





share|improve this answer





















  • 2





    Why downvoting? The answer is correct.

    – jarlh
    Dec 21 '15 at 8:55











  • Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

    – Lukasz Szozda
    Dec 21 '15 at 8:55








  • 1





    One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

    – Lukasz Szozda
    Dec 21 '15 at 9:02








  • 2





    It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

    – Andrew Morton
    Dec 21 '15 at 9:09











  • @AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

    – Lukasz Szozda
    Dec 21 '15 at 9:10


















11














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





share|improve this answer





















  • 2





    Why downvoting? The answer is correct.

    – jarlh
    Dec 21 '15 at 8:55











  • Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

    – Lukasz Szozda
    Dec 21 '15 at 8:55








  • 1





    One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

    – Lukasz Szozda
    Dec 21 '15 at 9:02








  • 2





    It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

    – Andrew Morton
    Dec 21 '15 at 9:09











  • @AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

    – Lukasz Szozda
    Dec 21 '15 at 9:10
















11












11








11







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 2 '18 at 17:42

























answered Dec 21 '15 at 8:51









Lukasz SzozdaLukasz Szozda

79.1k1061105




79.1k1061105








  • 2





    Why downvoting? The answer is correct.

    – jarlh
    Dec 21 '15 at 8:55











  • Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

    – Lukasz Szozda
    Dec 21 '15 at 8:55








  • 1





    One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

    – Lukasz Szozda
    Dec 21 '15 at 9:02








  • 2





    It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

    – Andrew Morton
    Dec 21 '15 at 9:09











  • @AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

    – Lukasz Szozda
    Dec 21 '15 at 9:10
















  • 2





    Why downvoting? The answer is correct.

    – jarlh
    Dec 21 '15 at 8:55











  • Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

    – Lukasz Szozda
    Dec 21 '15 at 8:55








  • 1





    One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

    – Lukasz Szozda
    Dec 21 '15 at 9:02








  • 2





    It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

    – Andrew Morton
    Dec 21 '15 at 9:09











  • @AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

    – Lukasz Szozda
    Dec 21 '15 at 9:10










2




2





Why downvoting? The answer is correct.

– jarlh
Dec 21 '15 at 8:55





Why downvoting? The answer is correct.

– jarlh
Dec 21 '15 at 8:55













Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

– Lukasz Szozda
Dec 21 '15 at 8:55







Thanks for downvotes. I will wait for Dynamic-SQL solution that will work for each RDBMS, check metadata and do trimming for string columns.

– Lukasz Szozda
Dec 21 '15 at 8:55






1




1





One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

– Lukasz Szozda
Dec 21 '15 at 9:02







One trick to do it faster in SSMS is to drag and drop from object explorer all columns to query window. Then move all string columns at the beginning, use block selection and write TRIM( for multiple columns at once. Then move to the end of line, block selection and close missing ). SQL Server has LTRIM(RTRIM( instead of TRIM.

– Lukasz Szozda
Dec 21 '15 at 9:02






2




2





It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

– Andrew Morton
Dec 21 '15 at 9:09





It would need LTRIM(RTRIM(...)) in T-SQL as that doesn't have TRIM.

– Andrew Morton
Dec 21 '15 at 9:09













@AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

– Lukasz Szozda
Dec 21 '15 at 9:10







@AndrewMorton Please refresh page. Last sentence :) I guess comments are not refreshed like answers.

– Lukasz Szozda
Dec 21 '15 at 9:10




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34391793%2fis-there-a-way-to-trim-all-data-in-a-select-from-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga