Search code examples
mysqlsubstringsubstr

MYSQL: multiple functions in query String


the following query is not working in MYSql (a Trigger)

set new.uniq = SUBSTR(md5(concat(new.lat, '-', new.lon)),0,5)

only if i delete the SUBSTR it will give me an correct output

set new.uniq = md5(concat(new.lat, '-', new.lon))

Solution

  • The problem is the zero:

    set new.uniq = SUBSTR(md5(concat(new.lat, '-', new.lon)),0,5)
                                                             ^
    

    For example:

    SELECT SUBSTR('whatever', 0, 5) --> returns empty string
    SELECT SUBSTR('whatever', 1, 5) --> returns 'whate'
    

    Change the zero to a 1 and you should be fine:

    set new.uniq = SUBSTR(md5(concat(new.lat, '-', new.lon)),1,5)