Is there a possibility to obtain letters (like A,B) instead of numbers (1,2) e.g. as a result of Dense_Rank function call(in MS Sql) ?
Not a direct answer - but if somebody has a alphabet conversion requirement with 3 characters, following is what I am doing.
/*
Function Desc: Convert integer value to 3 character alpha-numeric
--Note: 1. This will return unique values from 0 to 17575, after that it startes again from AAA.
2. Returns NULL If less than 0.
--Test Values
select dbo.udfGetBase26CharacterValue(0) --AAA
select dbo.udfGetBase26CharacterValue(17575) --ZZZ
select dbo.udfGetBase26CharacterValue(17576) --AAA
select dbo.udfGetBase26CharacterValue(NULL) --NULL
select dbo.udfGetBase26CharacterValue(-1) --NULL
*/
CREATE FUNCTION [dbo].udfGetBase26CharacterValue
(
@id INT
)
RETURNS CHAR(3)
AS
BEGIN
IF ((@id < 0) OR (@id IS NULL))
BEGIN
Return NULL
END
--Convert to base 26
Return char(@id / power(26,2) % 26 + 65) +
char(@id / 26 % 26 + 65) +
char(@id % 26 + 65)
END
Another approach - get next character code (it is alpha-numeric result). If you pass '00A' it will return '00B'
CREATE FUNCTION dbo.fnGetNextCharacterCode (@InputCode char(3))
RETURNS char(3)
AS
BEGIN
IF LEN(LTRIM(RTRIM(@InputCode))) = 2
BEGIN
SET @InputCode = '0'+LTRIM(RTRIM(@InputCode))
END
ELSE IF LEN(LTRIM(RTRIM(@InputCode))) = 1
BEGIN
SET @InputCode = '00'+LTRIM(RTRIM(@InputCode))
END
DECLARE @NewCode char(3)
SELECT @NewCode =
CASE WHEN RIGHT(@InputCode,2) != 'ZZ' THEN LEFT(@InputCode,1)
ELSE CHAR(
CASE LEFT(@InputCode,1) WHEN '9' THEN 64
WHEN 'Z' THEN 47
ELSE ASCII(LEFT(@InputCode,1)
)
END + 1
)
END ---First Char
+
CASE WHEN RIGHT(@InputCode,1) != 'Z' THEN SUBSTRING(@InputCode,2,1)
ELSE CHAR(
CASE SUBSTRING(@InputCode,2,1) WHEN '9' THEN 64
WHEN 'Z' THEN 47
ELSE ASCII(SUBSTRING(@InputCode,2,1))
END + 1
)
END ---Second Char
+
CHAR(CASE RIGHT(@InputCode,1) WHEN '9' THEN 64
WHEN 'Z' THEN 47
ELSE ASCII(RIGHT(@InputCode,1))
END + 1) ---Third Char
RETURN @NewCode
END
GO