Search code examples
javascriptmysqlsqldatetimeunix-timestamp

How do we convert time in unix timestamp to string with radix 32 in MySQL


I want to convert the current time in unix timestamp to a string with radix 32 in MySQL

It can be achieved in JavaScript with the code below

'm' + prefix + Date.now().toString(32) + suffix

Sample SQL to give the idea (working incorrectly )

SET prefix = 'order'
SET suffix = 19;
SELECT 'm' + prefix + CONV( UNIX_TIMESTAMP(NOW()), 10, 32 ) + suffix AS   result

Solution

  • + is considered an arithmetic operator in MySQL.

    You need to use CONCAT function to concatenate multiple strings like below:

    SELECT CONCAT('m' , prefix , CONV( UNIX_TIMESTAMP(), 10, 32 ) , suffix) AS   result
    

    Note: UNIX_TIMESTAMP() and UNIX_TIMESTAMP(NOW()) would return same value.

    TEST:

    SET @prefix := 'PRE';
    SET @suffix := 'SUF';
    
    SELECT CONCAT('m' , @prefix , CONV( UNIX_TIMESTAMP(), 10, 32 ) , @suffix) AS   result
    

    Output: mPRE1BSVENHSUF

    See Live Demo