且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在多个oracle中显示前导零

更新时间:2023-02-25 21:11:57

如果数字为0,则应为00000;如果数字为1,则应为00000 为00001,如果数字为12,则应为00012

If number is 0 then it should be 00000 If number is 1 then it should be 00001 If number is 12 the it should be 00012

记住:这里的00000,0000和00012是数字数据类型

Remember : here 00000,0000, and 00012 are of number datatypes

首先,数字没有以零开头的数字.因此,当您存储NUMBER值时,就让它们表现得像NUMBER.仅当要显示它们时,才可以使用LPAD并添加前导零.将数字转换为带前导零的字符串.

Firstly, Numbers don't have leading zero's. So, when you store the NUMBER values, you let them behave like NUMBERs. it is only when you want to display them, you can use LPAD and add the leading zeroes. Which conevrts the number to a string with leading zeroes.

因此,无需更新表.使用LPAD以所需的方式显示它们.

So, no need to update the table. Use LPAD to display them the way you want.

 SQL> WITH DATA AS
  2    ( SELECT 1 ID FROM DUAL UNION ALL
  3      SELECT 11 ID FROM DUAL
  4    )
  5  SELECT
  6     LPAD(ID,5, 0) id
  7  FROM DATA
  8  /

ID
-----
00001
00011

为避免隐式数据类型转换,请在应用LPAD之前使用TO_CHAR.

To avoid, implicit data type conversion, use TO_CHAR before applying LPAD.