格式化数字000..999

时间:2014-02-18 09:36:13

标签: sql oracle

我想格式化这样的数字:

000
001
002
003
004
.
.
.
010
011
.
111
.
999

3 个答案:

答案 0 :(得分:2)

试试这个pl / sql Block:

SET SERVEROUTPUT ON

BEGIN
  FOR i IN 1..999 LOOP
    DBMS_OUTPUT.PUT_LINE(To_CHAR(i, '009'));  
  END LOOP;
END;

答案 1 :(得分:1)

目前尚不清楚您的号码来自哪里。如果您只想动态生成列表,可以使用connect-by syntax

select to_char(level, 'FM00')
from dual
connect by level <= 10;

01
02
03
04
05
06
07
08
09
10

对于0到999,只需更改级别限制,format model即可获得所需的位数:

select to_char(level - 1, 'FM000')
from dual
connect by level <= 1000;

答案 2 :(得分:0)

SELECT ones.num + tens.num + hundreds.num
FROM
(SELECT 000 num UNION ALL
SELECT 10 num UNION ALL
SELECT 20 num UNION ALL
SELECT 30 num UNION ALL
SELECT 40 num UNION ALL
SELECT 50 num UNION ALL
SELECT 60 num UNION ALL
SELECT 70 num UNION ALL
SELECT 80 num UNION ALL
SELECT 90 num) ones
CROSS JOIN
(SELECT 0 num UNION ALL
SELECT 1 num UNION ALL
SELECT 2 num UNION ALL
SELECT 3 num UNION ALL
SELECT 4 num UNION ALL
SELECT 5 num UNION ALL
SELECT 6 num UNION ALL
SELECT 7 num UNION ALL
SELECT 8 num UNION ALL
SELECT 9 num) tens
CROSS JOIN
(SELECT 0 num UNION ALL
SELECT 100 num UNION ALL
SELECT 200 num UNION ALL
SELECT 300 num UNION ALL
SELECT 400 num UNION ALL
SELECT 500 num UNION ALL
SELECT 600 num UNION ALL
SELECT 700 num UNION ALL
SELECT 800 num UNION ALL
SELECT 900 num) hundreds;