oracle 数字格式上不需要的前导空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/156329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:18:37  来源:igfitidea点击:

unwanted leading blank space on oracle number format

sqloracle

提问by Ovesh

I need to pad numbers with leading zeros (total 8 digits) for display. I'm using oracle.

我需要用前导零(总共 8 位数字)填充数字以进行显示。我正在使用甲骨文。

select to_char(1011,'00000000') OPE_NO from dual;
select length(to_char(1011,'00000000')) OPE_NO from dual;

Instead of '00001011' I get ' 00001011'. Why do I get an extra leading blank space? What is the correct number formatting string to accomplish this?

我得到的不是“00001011”,而是“00001011”。为什么我会得到一个额外的前导空格?完成此操作的正确数字格式字符串是什么?

P.S. I realise I can just use trim(), but I want to understand number formatting better.

PS 我意识到我只能使用trim(),但我想更好地理解数字格式。

@Eddie: I already read the documentation. And yet I still don't understand how to get rid of the leading whitespace.

@Eddie:我已经阅读了文档。然而我仍然不明白如何摆脱领先的空白。

@David: So does that mean there's no way but to use trim()?

@David:那么这是否意味着除了使用之外别无他法trim()

回答by Steve Bosman

Use FM (Fill Mode), e.g.

使用 FM(填充模式),例如

select to_char(1011,'FM00000000') OPE_NO from dual;

select to_char(1011,'FM00000000') OPE_NO from dual;

回答by VVS

From that same documentationmentioned by EddieAwad:

EddieAwad提到的同一文档中:

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.

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



EDIT: The right way is to use the FM modifier, as answered by Steve Bosman. Read the section about Format Model Modifiersfor more info.

编辑:正确的方法是使用 FM 修饰符,正如史蒂夫博斯曼所回答的那样。阅读关于格式模型修饰符的部分以获取更多信息。