简体   繁体   English

想要在Oracle number数据类型列中将数据插入为“ 001”而不是“ 1”

[英]Want to insert data in oracle number data type column as '001' instead of '1'

Want to insert data in oracle number data type column as '001' instead of '1'. 想要将数据插入到“ oracle number”数据类型列中,而不是“ 001”,而不是“ 1”。

Here is my code: 这是我的代码:

create table TEMP2 (id number);

INSERT INTO TEMP2 VALUES(001);

When executing: 执行时:

SELECT * FROM TEMP2;

The output appears as: 输出显示为:

ID
1

I want to store number as '001' instead of '1'. 我想将数字存储为“ 001”而不是“ 1”。

You shouldn't store information that is only used for display purposes. 您不应存储仅用于显示目的的信息。 If that is a number, then by all means store it as a number. 如果那是数字,则一定要将其存储为数字。

You can always format the output when displaying the values: 显示值时,您始终可以格式化输出

SELECT to_char(id, 'FM000')
FROM TEMP2;

If you don't want to do that each time you run a select, create a view: 如果不想每次运行select时都这样做,请创建一个视图:

create view formatted_temp
as
SELECT to_char(id, 'FM000')
FROM TEMP2;

Or create a computed column that does that for you: 或创建一个为您执行此操作的计算列:

alter table temp2 
    add formatted_id generated always as (to_char(id, 'FM000'));

You can change the column to VARCHAR2. 您可以将列更改为VARCHAR2。 Wether this is a good solution depends on your needs 这是否是一个好的解决方案取决于您的需求

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

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