Please, take a look here:
Custom Auto-Generated Sequences with SQL Server - SQLTeam.com[
^]
There you'll find few very interesting functions with detailed information about algorithm.
All what you need to do is to get one of existing functions and to change to your needs.
Here is an idea:
;WITH CTE AS
(
SELECT 0 CurrNo
UNION ALL
SELECT CurrNo+1
FROM CTE
WHERE CurrNo+1<100
)
SELECT CurrNo, char(CurrNo /26 % 26 + 65) + char(CurrNo % 26 + 65) CurrCode
FROM CTE
Result:
CurrNo CurrCode
0 AA
1 AB
2 AC
3 AD
4 AE
5 AF
6 AG
7 AH
8 AI
9 AJ
10 AK
11 AL
12 AM
13 AN
...
96 DS
97 DT
98 DU
99 DV
SQL Server 2019 | db<>fiddle[
^]
So, to get digit representation od code, you need to convert it:
;WITH InitData AS
(
SELECT '20AZ' Sequence_Code
)
SELECT a.*, char((SumOfDigits +1) /26 % 26 + 65) + char((SumOfDigits +1) % 26 + 65) NewCode
FROM
(
SELECT Sequence_Code, LEFT(Sequence_Code, 2) CurrentYear,
RIGHT(Sequence_Code, 2) Curr_Code,
ASCII(SUBSTRING(Sequence_Code, 3, 1))-65 FirstDigitOfCode,
ASCII(RIGHT(Sequence_Code, 1))-65 SecondDigitOfCode,
ASCII(SUBSTRING(Sequence_Code, 3, 1))-65 +
ASCII(RIGHT(Sequence_Code, 1))-65 SumOfDigits
FROM InitData
) a
Result:
Sequence_Code CurrentYear Curr_Code FirstDigitOfCode SecondDigitOfCode SumOfDigits NewCode
20AZ 20 AZ 0 25 25 BA
db<>fiddle#2[
^]
Good luck!