﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[ott_b26]
(
	@number int
)
RETURNS varchar(10)
AS
BEGIN
	DECLARE @RET varchar(10)

	/*
		SELECT
		   *,
		   LetterCode =
			  Coalesce((SELECT Char(65 + (N.Num - 475255) / 456976 % 26) WHERE N.Num >= 475255), '')
			  + Coalesce((SELECT Char(65 + (N.Num - 18279) / 17576 % 26) WHERE N.Num >= 18279), '')
			  + Coalesce((SELECT Char(65 + (N.Num - 703) / 676 % 26) WHERE N.Num >= 703), '')
			  + Coalesce((SELECT Char(65 + (N.Num - 27) / 26 % 26) WHERE N.Num >= 27), '')
			  + (SELECT Char(65 + (N.Num - 1) % 26))
		FROM (VALUES(18931)) as N(Num)
		ORDER BY N.Num
	*/

	SET @RET = LOWER(
			  Coalesce((SELECT Char(65 + (@number - 475255) / 456976 % 26) WHERE @number >= 475255), '')
			  + Coalesce((SELECT Char(65 + (@number - 18279) / 17576 % 26) WHERE @number >= 18279), '')
			  + Coalesce((SELECT Char(65 + (@number - 703) / 676 % 26) WHERE @number >= 703), '')
			  + Coalesce((SELECT Char(65 + (@number - 27) / 26 % 26) WHERE @number >= 27), '')
			  + (SELECT Char(65 + (@number - 1) % 26))
			  );
	
	RETURN @RET;

END