为什么多了一个空格?
DBA » http://www.anysql.net/dba/why_extra_space_char.html 2008-01-11昨天要用SQL来获得一个分区的名称, 却发现生成的分区名中总是多了一个空格, 不知道原因何在, 只是数据库的字符集是UTF8. 如下所示:
18:50:50 SQL> SELECT TO_CHAR(9,'09') FROM DUAL;
TO_
---
09
用于获得分区名的语句, 其中9是根据一个公式算出来的值.
18:51:23 SQL> SELECT 'P'||TO_CHAR(9,'09') FROM DUAL;
'P'|
----
P 09
是显示问题? 不是, DUMP函数显示就是多了一个空格.
18:52:45 SQL> SELECT DUMP(TO_CHAR(9,'09')) coldump from dual;
COLDUMP
----------------------------------------
Typ=1 Len=3: 32,48,57
还有其他人遇到过吗? 帮忙测试一下? 用Perl或Java程序去执行这个查询, 返回值中一样多了个空格.


trim或ltrim一下
后来是那样解决的, 只是从来没有注意到过这个问题, 从DATE向CHAR转换的多, 为NUMBER指定格式情况的很少.
SQL> SELECT ‘P’||TO_CHAR(9,’FM09′) FROM DUAL;
‘P’|
----
P09
SQL> SELECT DUMP(TO_CHAR(9,’FM09′)) coldump from dual;
COLDUMP
--------------------------------------------------------------------------------
Typ=1 Len=2: 48,57
FM
Fill mode. Oracle uses blank characters to fill format elements to a constant width equal to the largest element for the relevant format model in the current session language. For example, when NLS_LANGUAGE is AMERICAN, the largest element for MONTH is SEPTEMBER, so all values of the MONTH format element are padded to 9 display characters. This modifier suppresses blank padding in the return value of the TO_CHAR function:
In a datetime format element of a TO_CHAR function, this modifier suppresses blanks in subsequent character elements (such as MONTH) and suppresses leading zeroes for subsequent number elements (such as MI) in a date format model. Without FM, the result of a character element is always right padded with blanks to a fixed length, and leading zeroes are always returned for a number element. With FM, which suppresses blank padding, the length of the return value may vary.
In a number format element of a TO_CHAR function, this modifier suppresses blanks added to the left of the number, so that the result is left-justified in the output buffer. Without FM, the result is always right-justified in the buffer, resulting in blank-padding to the left of the number.
谢谢adam的精彩回答.
不错,转载了
SQL> select dump(to_char(9,’99′)) from dual;
DUMP(TO_CHAR(9,’99′))
----------------------------------------------------------------------------------------------------
Typ=1 Len=3: 32,32,57
应该是用0来补位吧