如何在两个数字之间生成一系列字母数字?

How to generate a range of Alpha numeric numbers between two numbers?

我有 AP80000-AP81000 的号码列表。我需要查询以获取连续号码 liske A80000 A80001 A80002 A80003 等等

您需要从给定的字符串中找到您的号码范围,您可以使用 substringcharindex 获得该字符串。尽管还有其他各种方法可以实现这一目标。但是,例如,我们将其作为尝试。

declare @str nvarchar(max) = 'AP80000-AP81000'
declare @start int 
declare @end int

select 
@start = SUBSTRING( @str,3, charindex('-',@str)-3) , 
@end = SUBSTRING( @str , CHARINDEX('-',@str)+3, 5) 

之后,通过使用 recursive cte,您将生成此范围内的数字。您会找到有关此 link.

的更多信息

;WITH gen AS (
    SELECT @start AS NUM
    UNION ALL
    SELECT num+1 FROM gen WHERE num+1<=@end
)
select NUM from gen
option (maxrecursion 10000)

最后,您将在生成的数字前面连接您的后缀以获得您想要的结果。

最终代码块将如下所示。

declare @str nvarchar(max) = 'AP80000-AP81000'
declare @start int 
declare @end int
select 
@start = SUBSTRING( @str,3, charindex('-',@str)-3) , 
@end = SUBSTRING( @str , CHARINDEX('-',@str)+3, 5) 

;WITH gen AS (
    SELECT @start AS num
    UNION ALL
    SELECT num+1 FROM gen WHERE num+1<=@end
)
select CONCAT('AP', num) from gen
option (maxrecursion 10000)