为什么 TO_CHAR 在数字开头添加空格(一个白色字符)? [英] Why is TO_CHAR adding space (one white char) at the beginning of a number?

查看:59
本文介绍了为什么 TO_CHAR 在数字开头添加空格(一个白色字符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我根据此代码准备此 SQL Fiddle 时:

As I prepared this SQL Fiddle based on this code:

WITH l_cur AS
 (SELECT to_char(LEVEL - 1, '0000') dd FROM dual CONNECT BY LEVEL <= 10)
SELECT '''' || d1.dd || '''' o,
       '''' || d2.dd || '''' n
  FROM (SELECT dd,
               row_number() over(ORDER BY 1) rn
          FROM l_cur) d1,
       (SELECT dd,
               row_number() over(ORDER BY dbms_random.random) rn
          FROM l_cur) d2
 WHERE d1.rn = d2.rn;

O       N
------- -------
' 0000' ' 0000'
' 0001' ' 0005'
' 0002' ' 0009'
' 0003' ' 0003'
' 0004' ' 0002'
' 0005' ' 0008'
' 0006' ' 0006'
' 0007' ' 0001'
' 0008' ' 0007'
' 0009' ' 0004'
10 rows selected

函数 to_char 是在开头添加一个空白字符 ' '(空格).这是 Oracle 错误吗?

To function to_char is adding one blank character ' ' (space) at beginning. Is this an Oracle bug?

推荐答案

不,这不是错误,而是 记录的行为.

No, it isn't a bug, it's the documented behaviour.

负返回值自动包含前导负号和正值自动包含前导空格,除非格式模型包含 MI、S 或 PR 格式元素.

Negative return values automatically contain a leading negative sign and positive values automatically contain a leading space unless the format model contains the MI, S, or PR format element.

您可以使用格式模型修饰符 改变这种行为;在这种情况下,FM '填充模式' 修饰符(尽管文档并没有真正讨论它与数字格式模型的使用):

You can use a format model modifier to change this behaviour; in this case the FM 'fill mode' modifier (although the documentation doesn't really talk about its use with number format models):

SELECT to_char(LEVEL - 1, 'fm0000') ...

SQL 小提琴.

这篇关于为什么 TO_CHAR 在数字开头添加空格(一个白色字符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆