I am barely learning MySQL (moving from SQLite). I am trying to create a custom function within the MySQL command line client, but am hitting errors that I can’t correct – and can’t find answers to online. My first function began as this:
CREATE FUNCTION myDatabase.LCASE_ASCII_VALUE_AT(
unparsedLine VARCHAR(1024),
linePos int
) RETURNS int DETERMINISTIC
return ASCII( LCASE( RIGHT( CHAR_LENGTH(unparsedLine) - linePos + 1) ) );
I got this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') )' at line 10
Any pointers on where my syntax went wrong? I suspected these, but they didn’t fix the problem:
- Perhaps MySQL does not like compound statements like
return ASCII( LCASE( .... But even after manually unraveling the statement, I still got the same error (but on a line in the newly unraveled statement – I leave out the unraveled statement to save space). - Perhaps the
DELIMITER $$ [function definition] END $$ DELIMITER ;business is mandatory – but that didn’t fix the error, and MySQL documentation gives an example of a one-line function where DELIMITER is not needed. (the ‘hello’ function at https://dev.mysql.com/doc/refman/5.0/en/create-procedure.html)
EDIT: Added that I am creating this function within MySQL command line client.
rightrequires two arguments, the string itself and the number of characters. You’ve only given the character count.Try this instead:
Also, you may find that
substris a better option since there’s no need to calculate the argument torightin that case, something like: