mysqlbinaryuuidmysql-5.7mysql-function

How to pass binary (16) to a mysql function as an argument


I would like to pass the uuid of version varbinary(16) to a user-defined function in MySQL version 5.7.x - It throws an error

1406 - Data too long for column

I have a table with uuid primary key of type varbinary(16) - I would like to convert the binary(16) to human readable version so I written a function to convert the binary version.

CREATE DEFINER=`bala`@`localhost` 
FUNCTION `bin_to_uuid`(`uuid` BINARY(32)) 
    RETURNS varchar(32) CHARSET latin1
NO SQL
return LOWER(CONCAT(
    SUBSTR(HEX(uuid), 1, 8), '-',
    SUBSTR(HEX(uuid), 9, 4), '-',
    SUBSTR(HEX(uuid), 13, 4), '-',
    SUBSTR(HEX(uuid), 17, 4), '-',
    SUBSTR(HEX(uuid), 21)
))

SELECT bin_to_uuid((UNHEX(REPLACE(uuid(), "-",""))))

MySQL version 5.7.x


Solution

  • The return type is varchar(32), but with the hyphens added, the text you're returning is 36 chars long.

    Change the return type to varchar(36).