我有一个sqlplus假脱机(oracle 12c / 18c)问题。我要在列中修剪额外的空白。
这是预期的结果
01JHON BROWN 30RED
02MARIO ROSSI 25WHITE
这是我的结果
01 JHON BROWN 30 RED
02 MARIO ROSSI 25 WHITE
这是sql代码
SET ECHO OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET SERVEROUTPUT ON
SET HEADING OFF
SET PAGESIZE 0
SET LINESIZE 2000
SET SQLBLANKLINES ON
SET FEEDBACK OFF
SET TIME OFF
SET TIMING OFF
SET COLSEP ''
SET TRIMSPOOL OFF
SET TERMOUT OFF
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD';
ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,';
spool pippo.txt
SELECT TRIM(NUM), RPAD(NAME,12), TRIM(AGE), RPAD(COLOR,7)
FROM PLUTO;
spool off
exit
THX
这就是spool
的列格式的工作方式-每行的值都填充到列的整个宽度。 See this similar question了解您的选择。
如果您不希望在列值之间有任何空格,则通常必须将它们连接到单个列中,例如。
SELECT TRIM(NUM) || RPAD(NAME,12) || TRIM(AGE) || RPAD(COLOR,7)
FROM PLUTO;
尝试
SELECT TRIM(NUM)||RPAD(NAME,12)||TRIM(AGE)||RPAD(COLOR,7)
FROM PLUTO;
这样您将不会在值之间添加任何空格。
例如:
SQL> select employee_id, rtrim(first_name) first, rtrim(last_name) last
2 from employees
3 fetch first 10 rows only;
EMPLOYEE_ID FIRST LAST
----------- -------------------- -------------------------
100 Steven King
101 Neena Kochhar
102 Lex De Haan
103 Alexander Hunold
104 Bruce Ernst
105 David Austin
106 Valli Pataballa
107 Diana Lorentz
108 Nancy Greenberg
109 Daniel Faviet
10 rows selected.
SQL> select employee_id||first_name||last_name from employees
2 fetch first 10 rows only;
EMPLOYEE_ID||FIRST_NAME||LAST_NAME
--------------------------------------------------------------------------------
100StevenKing
101NeenaKochhar
102LexDe Haan
103AlexanderHunold
104BruceErnst
105DavidAustin
106ValliPataballa
107DianaLorentz
108NancyGreenberg
109DanielFaviet
10 rows selected.