简体   繁体   English

Oracle Number数据类型问题

[英]Oracle number data type issue

I'm trying to save a number of certain characters(let's say 4) in my oracle 11g db. 我正在尝试在Oracle 11g数据库中保存一些特定字符(假设为4)。 My numbers has a leading zero such 0100 0101 etc when i say the data. 当我说数据时,我的数字有一个前导零,例如0100 0101等。 Why does oracle truncate the leading zero. oracle为什么截断前导零。 Is it possible to avoid this. 是否有可能避免这种情况。 I really want the leading zero to get saved. 我真的很想保存前导零。 Please help. 请帮忙。

Thanks! 谢谢!

If you want to display the number with a leading zero then use: 如果要显示以零开头的数字,请使用:

to_char(my_number,'fm0000')

If you really need to store the number with a leading zero then store it as a varchar2, because what you have there is a string that consists only of digits, not a number. 如果您确实需要将数字存储在前导零处,则将其存储为varchar2,因为您所拥有的字符串是仅包含数字而不是数字的字符串。

The TO_CHAR answers above are best, but they need a tweak because TO_CHAR(number, '0000') will add leading zeros plus a leading space, which is a placeholder in case there's a negative sign: 上面的TO_CHAR答案是最好的,但是它们需要进行调整,因为TO_CHAR(number, '0000')将添加前导零前导空格,如果存在负号,则为占位符:

SELECT '[' || TO_CHAR(123, '0000') || ']' "Format" FROM DUAL;

Format
-------
[ 0123] <-- note the leading space

To get rid of the space, either LTRIM or use the FM qualifier in the format string: 要摆脱空间,请使用LTRIM或在格式字符串中使用FM限定符:

SELECT '[' || TO_CHAR(123, 'FM0000') || ']' "Format" FROM DUAL;

Format
-------
[0123] <-- no more leading space

If you want to store the leading zero, then store the number as a char() for fixed length values or varchar() (or varchar2() ) for variable length values. 如果要存储前导零,则将数字存储为固定长度值的char()或可变长度值的varchar() (或varchar2() )。

Numbers are stored in a binary format, which has no concept of leading zeros. 数字以二进制格式存储,没有前导零的概念。

获得8位数字的简单技巧:

substr(your_number + power(10,8), 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM