我需要编写一个SQL查询以在SQL中打印以下非字母序列
0001,0002,…,0009,000A,…,000Z,…,0010,0011,…,001A,…等等,直到…,ZZZZ
请注意:所有字符均大写。
提前致谢
您可以创建如下函数:
create function to_base_36 (n integer) return varchar2 is q integer; r varchar2(100); begin q := n; while q >= 36 loop r := chr(mod(q,36)+case when mod(q,36) < 10 then 48 else 55 end) || r; q := floor(q/36); end loop; r := chr(mod(q,36)+case when mod(q,36) < 10 then 48 else 55 end) || r; return lpad(r,4,'0'); end;
然后像这样使用它:
select rownum, to_base_36(rownum) from dual connect by level < 36*36*36*36;
或者,不创建函数:
with digits as ( select n, chr(mod(n,36)+case when mod(n,36) < 10 then 48 else 55 end) d from (Select rownum-1 as n from dual connect by level < 37) ) select d1.n*36*36*36 + d2.n*36*36 + d3.n*36 + d4.n, d1.d||d2.d||d3.d||d4.d from digits d1, digits d2, digits d3, digits d4