如何将oracle数字类型转换为格式的字符串?

时间:2013-08-08 10:49:27

标签: string oracle numbers format converter

我想将数字类型转换为字符串格式为:

number -> string

    1      -> 001
    2      -> 002
    12     -> 012
    340    -> 340  

1 个答案:

答案 0 :(得分:7)

您可以使用TO_CHAR()(在这种情况下最好)功能或LPAD()功能来获得所需的结果:

SQL> with t1(col) as(
  2    select 1   from dual union all
  3    select 2   from dual union all
  4    select 12  from dual union all
  5    select 340 from dual
  6  )
  7  select to_char(col, '000')        as num_1
  8       , lpad(to_char(col), 3, '0') as num_2
  9    from t1
 10  ;

NUM_1 NUM_2
----- ------------
 001  001
 002  002
 012  012
 340  340