DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Issue with MySQL db Function and rand()  (Read 4,949 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Issue with MySQL db Function and rand()
« on: February 10, 2015, 09:40 PM »
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?
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;
END


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

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: Issue with MySQL db Function and rand()
« Reply #1 on: February 11, 2015, 01:13 PM »
Okay, fine ... I'll answer my own question. Here's what finally worked:
Code: (sql)
CREATE DEFINER=`Stoic Joker`@`%` FUNCTION `MakeUniID`() RETURNS char(7) CHARSET utf8
BEGIN
declare Result CHAR(7);
SET Result = '';

SET Result=CONCAT(
	ELT(FLOOR(1 + (RAND() * (26-1))), 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' ),
	ELT(FLOOR(1 + (RAND() * (26-1))), 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' ),
	ELT(FLOOR(1 + (RAND() * (26-1))), 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' ),
	ELT(FLOOR(1 + (RAND() * (10-1))), '0','1','2','3','4','5','6','7','8','9' ),
	ELT(FLOOR(1 + (RAND() * (10-1))), '0','1','2','3','4','5','6','7','8','9' ),
	ELT(FLOOR(1 + (RAND() * (10-1))), '0','1','2','3','4','5','6','7','8','9' ),
	ELT(FLOOR(1 + (RAND() * (10-1))), '0','1','2','3','4','5','6','7','8','9' )
	);

RETURN Result;
END

^Which I ended up finding here (for anyone else that might end up looking for similar).

Original address: https://www.donationcoder.com/forum/index.php?topic=40202.0