Create leading zero in Oracle

时间秒杀一切 提交于 2019-12-31 02:59:06

问题


I am using Adempiere which has database Oracle

I have window called Stock Code from table called M_StockCode

The fields are Code and Description.

Currently, Code data type is Number and Description is Varchar2

I want to input Sparepart with Code 01, and Body Repair with Code 02.

As I input the data in Adempiere and save it, what will show is Sparepart with Code 1 (without leading zero)

I've tried putting LPAD function but it's still failed.

How can I put 01 both in Adempiere interface and in database?

Any suggestion will be appreciated :)


回答1:


A NUMBER cannot have leading zero, a STRING can.

  1. If you want to store the codes with leading zero in the database table, then you must use VARCHAR2 and not NUMBER.

  2. If you want to just display the number with leading zero, then use TO_CHAR to convert the number into string.

For example,

SQL> SELECT TO_CHAR(1, '00') FROM DUAL;

TO_
---
 01

You could also use LPAD, but remember, the data type of the result would be a string and not a number.

For example,

SQL> SELECT LPAD(1, 2, '0') FROM DUAL;

LP
--
01



回答2:


In Adempiere you manage the model via the Application Dictionary, it's the equivalent of the Data Dictionary used by relational databases.

  1. Login to Adempiere as the System Administrator.
  2. Select menu option Application Dictionary->Table & Column.
  3. Select the M_StockCode table and from its window, click on the Columns Tab.
  4. Here, select the Code column and if need be toggle to the Form view
  5. Now, from the drop-down select the Reference value of String and set a Length equal 2.
  6. Finally, hit the Synchronize Column button to have Admepiere modify the database.

If this code is some kind of classification it might be useful to create new Reference type in the Application Dictionary and then you could point your column to it; this would ensure consistency, offering the user a selection to choose from rather than entering values. A Reference can be a List type which is handy for short lists or Table driven which is useful when the classification is more volatile and needs to maintained by the users.



来源:https://stackoverflow.com/questions/33051081/create-leading-zero-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!