Listing 7: Set-Based Solution That Returns Values from T1 in Base 36, Abbreviated DECLARE @b AS int; SET @b = 36; SELECT id, decval, MAX(CASE WHEN pos = 63 THEN digit ELSE '' END) + /* ... digits 21 through 62 go here ... */ MAX(CASE WHEN pos = 20 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 19 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 18 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 17 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 16 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 15 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 14 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 13 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 12 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 11 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 10 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 9 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 8 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 7 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 6 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 5 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 4 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 3 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 2 THEN digit ELSE '' END) + MAX(CASE WHEN pos = 1 THEN digit ELSE '' END) AS targetval FROM (SELECT id, decval, pos, SUBSTRING('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', decval / val % @b + 1, 1) AS digit FROM T1 JOIN BasePowers ON val <= decval WHERE base = @b) AS D GROUP BY id, decval;