if exists (select * from sys.objects where object_id = OBJECT_ID(N'[sysdba].[GetPrettyKeySuffix]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) drop function sysdba.GetPrettyKeySuffix go create function sysdba.GetPrettyKeySuffix ( @id varchar(12) ) returns varchar(24) as begin declare @key varchar(24) select @key = cast(id.digit1 as bigint)*power(36,0) + cast(id.digit2 as bigint)*power(36,1) + cast(id.digit3 as bigint)*power(36,2) + cast(id.digit4 as bigint)*power(36,3) + cast(id.digit5 as bigint)*power(36,4) + cast(id.digit6 as bigint)*power(36,5) from ( select case when substring(@id, len(@id)-0, 1) like '[0-9]' then substring(@id, len(@id)-0, 1) else ascii(substring(@id, len(@id)-0, 1)) - ascii('A')+10 end as digit1 , case when substring(@id, len(@id)-1, 1) like '[0-9]' then substring(@id, len(@id)-1, 1) else ascii(substring(@id, len(@id)-1, 1)) - ascii('A')+10 end as digit2 , case when substring(@id, len(@id)-2, 1) like '[0-9]' then substring(@id, len(@id)-2, 1) else ascii(substring(@id, len(@id)-2, 1)) - ascii('A')+10 end as digit3 , case when substring(@id, len(@id)-3, 1) like '[0-9]' then substring(@id, len(@id)-3, 1) else ascii(substring(@id, len(@id)-3, 1)) - ascii('A')+10 end as digit4 , case when substring(@id, len(@id)-4, 1) like '[0-9]' then substring(@id, len(@id)-4, 1) else ascii(substring(@id, len(@id)-4, 1)) - ascii('A')+10 end as digit5 , case when substring(@id, len(@id)-5, 1) like '[0-9]' then substring(@id, len(@id)-5, 1) else ascii(substring(@id, len(@id)-5, 1)) - ascii('A')+10 end as digit6 ) id if len(@key) < 6 set @key = right('000000'+@key, 6) return @key end go