I need to create, and then store in the db table, an alpha representation of a primary key.
I found a wonderful PHP script that does exactly what I want. However, I am not sure of the best way to implement it directly in SQL Server.
I believe that I should be using a persistent Computed Column..? However, I am not sure how to convert the PHP function into one that work in SQL Server. Any assistance/education would be greatly appreciated.
The PHP code:
function alphaID($n)
{
for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) {
$r = chr($n%26 + 0x41) . $r;
}
return str_pad($r, 6, 'A', STR_PAD_LEFT);
}
echo alphaID(0) . "<br>"; // returns "AAAAAA"
echo alphaID(1) . "<br>"; // returns "AAAAAB"