Greetings
I've been fighting with this for a day or so and it giving me fits. I'm trying to get a MySQL db function to automatically generate a 7 character ID string during an insert command in the format of ABC1234. At this point triggering the function during insert is working fine, however the string generated is randomly - generated to short - between 4 and 7 characters in length.
So my question is how to I force this code to always give my a 7 character long string?
It seemed to be a guaranteed no brainer when I swiped it from somewhere - and then modified it for my own purposes... But the damn thing just won't cooperate.
Thanks in Advance,
Stoic Joker
I've been fighting with this for a day or so and it giving me fits. I'm trying to get a MySQL db function to automatically generate a 7 character ID string during an insert command in the format of ABC1234. At this point triggering the function during insert is working fine, however the string generated is randomly - generated to short - between 4 and 7 characters in length.
So my question is how to I force this code to always give my a 7 character long string?
Code: (sql)
CREATE DEFINER=`Stoic Joker`@`%` FUNCTION `MakeUniID`() RETURNS char(7) CHARSET utf8
BEGIN
declare Result CHAR(7);
SET Result = '';
SET Result=CONCAT(
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand(@seed:=round(rand(@seed)*4294967296))*26+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand(@seed:=round(rand(@seed)*4294967296))*26+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand(@seed:=round(rand(@seed)*4294967296))*26+1, 1),
substring('0123456789', rand(@seed:=round(rand(@seed)*4294967296))*10+1, 1),
substring('0123456789', rand(@seed:=round(rand(@seed)*4294967296))*10+1, 1),
substring('0123456789', rand(@seed:=round(rand(@seed)*4294967296))*10+1, 1),
substring('ABCDEFGHIJ', rand(@seed)*10+1, 1)
);
RETURN Result;
ENDIt seemed to be a guaranteed no brainer when I swiped it from somewhere - and then modified it for my own purposes... But the damn thing just won't cooperate.
Thanks in Advance,
Stoic Joker



