简体   繁体   English

如何在DB2中生成一个字母数字序列?

[英]How to generate an alphanumeric sequence in DB2?

Is it possible to generate an alphanumeric sequence in DB2? 是否可以在DB2中生成字母数字序列?

I want to create an alphanumeric sequence which starts from 'AA' and ends with '99', as follows: 我想创建一个以“ AA”开头并以“ 99”结尾的字母数字序列,如下所示:

AA...AZ, A0...A9, BA...BZ, B0...B9, ... ZA...ZZ, 
Z0...Z9, 0A...0Z,   00...09, 1A...1Z, 10...19, ...9A...9Z, 90...99

Here is a recursive query that iterates from 0 to 1295 and DIV & MOD functions to lookup the alpha characters: 这是一个递归查询,它从0迭代到1295,并使用DIV和MOD函数查找字母字符:

WITH n(n,x) AS
(
SELECT 0,'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT n+1,x FROM n WHERE n<1295
)
SELECT 
    SUBSTR(x, n/36 + 1,1) || SUBSTR(x, MOD(n,36) + 1,1) 
FROM n

DB2 has the CREATE SEQUENCE statement to allow the creation of numeric sequences. DB2具有CREATE SEQUENCE语句,以允许创建数字序列。 There is functionality to obtain the current and the next value of defined sequences. 具有获取定义序列的当前值和下一个值的功能。 Create a SQL function that, when invoked, obtains the next value from your sequence and translates it into your specific encoding. 创建一个SQL函数 ,该函数在被调用时从序列中获取下一个值,并将其转换为您的特定编码。

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

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