MySQL Forums
Forum List  »  Newbie

Re: How to create a function to evaluate numeric expression
Posted by: Rick James
Date: December 18, 2014 04:57PM

The quotes matter:
mysql> SELECT '(12+45+8)/10', (12+45+8)/10;
ERROR 2006 (HY000): MySQL server has gone away
+--------------+--------------+
| (12+45+8)/10 | (12+45+8)/10 |
+--------------+--------------+
| (12+45+8)/10 |       6.5000 |
+--------------+--------------+

Stored Functions may be handy:
mysql> CREATE FUNCTION Ft_to_Inches(_ft DOUBLE) RETURNS DOUBLE
    -> DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
    -> RETURN _ft * 12;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT Ft_to_Inches(123.456);
+-----------------------+
| Ft_to_Inches(123.456) |
+-----------------------+
|              1481.472 |
+-----------------------+
1 row in set (0.01 sec)

If you need the end-user to type in arbitrary expressions, that is asking for a hacker attack. Don't do it unless you are willing to check the input.

Options: ReplyQuote


Subject
Written By
Posted
Re: How to create a function to evaluate numeric expression
December 18, 2014 04:57PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.