The system I am working with has a numbering system where the numbers 0-999
are represented by the usual 0-999
, but 1000
is represented by A00
, followed by A01
, A02
, A03
, etc, 1100
being B00
etc.
I can't think of a way to handle this in T-SQL without resorting to inspecting individual digits with huge case statements, and there must be a better way than that. I had thought about using Hexadecimal but that's not right.
DECLARE @startint int = 1,
@endint int = 9999;
;WITH numbers(num)
AS
(
SELECT @startint AS num
UNION ALL SELECT num+1 FROM numbers
WHERE num+1 <= @endint
)
SELECT num, convert(varbinary(8), num) FROM [numbers] N
OPTION(MAXRECURSION 0)
With this 999
is now 3E7
, where it should just be 999
.
This currently produces this:
Number Sequence
0 0x00000000
1 0x00000001
...
10 0x0000000A
...
100 0x00000064
...
999 0x000003E7
1000 0x000003E8
What I'm looking for:
Number Sequence
0 000
1 001
...
10 010
11 011
12 012
...
999 999
1000 A00
1001 A01
...
1099 A99
1100 B00
1101 B01
1200 C00
I need this to work in SQL Server 2008.
IDENTITY
column would be enough as long as you got the formatting part right