我需要创建一个自定义序列,该序列应为以下范围创建序列
XV00AA-XV99ZZ
和许多其他范围
示例:
XV00AA, XV01AA, XV02AA, ......XV99AA
前两个字符保持不变(示例-XV系列);最后2个字符保持不变(示例-AA系列);但中间的2个字符应从0到99递增。(例如XV01AA, XV02AA, XV03AA
,依此类推)
一旦达到99(即XV99AA
),就应该对AB系列重复
因此输出应为
XV00AB, XV01AB, XV02AB, .....XV99AB
然后
XV00AC,XV01AC,XV02AC....XV99AC
因此示例最终输出:
XV00AA
XV01AA
XV02AA
....
XV99AA
XV00AB
XV01AB
XV02AB
....
XV99AB
XV00AC
XV01AC
XV02AC
...
XV99AC
XV00AD
XV01AD
XV02AD
...
XV99AD
,依此类推。有没有简单的方法来创建这些系列?任何帮助将不胜感激
答案 0 :(得分:3)
这应该可以解决问题。
DECLARE @how_many_do_you_want INT = 67599; -- 67599 is where it runs out of legit values.
WITH
cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),
cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),
cte_n3 (n) AS (SELECT 1 FROM cte_n2 a CROSS JOIN cte_n2 b),
cte_Tally (n) AS (
SELECT TOP (@how_many_do_you_want)
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM
cte_n3 a CROSS JOIN cte_n3 b
)
SELECT
rn = t.n,
CONCAT('XV', cn.char_num, a.alpha_1, a.alpha_2)
FROM
cte_Tally t
CROSS APPLY ( VALUES (t.n % 100) ) m (mod_100)
CROSS APPLY ( VALUES (((t.n - m.mod_100) / 100) % 26 + 1) ) g1 (group_1)
CROSS APPLY ( VALUES (t.n / 2600 + 1) ) g2 (group_2)
CROSS APPLY ( VALUES (RIGHT(CONCAT('0', m.mod_100), 2)) ) cn (char_num)
CROSS APPLY ( VALUES (CHAR(g1.group_1 + 64), CHAR(g2.group_2 + 64)) ) a (alpha_1, alpha_2);
答案 1 :(得分:1)
以下代码使用CTE生成一个从0到67,599的数字表。然后将这些值分开:模数(%
)提供数字的值,而整数除法(/
)提供字母的值。格式化和转换有点麻烦,鲍勃是你的叔叔。
with Numbers as (
select 0 as N
union all
select N + 1
from Numbers
where N < 67599 )
select N, N % 100 as DigitsValue, N / 100 as LettersValue,
Right( '0' + Cast( N % 100 as VarChar(2) ), 2 ) as LeftPaddedDigits,
Char( ASCII( 'A' ) + ( N / 100 ) % 26 ) as LeastSignificantLetter,
Char( ASCII( 'A' ) + ( N / 100 ) / 26 ) as MostSignificantLetter
from Numbers
option ( MaxRecursion 0 )
将完整的字符串连同“ XV”作为前缀一起留给读者。