Re: How to check if a variable is numeric in MySQL
Hi Javin,
You can choose between client side and server side conversions here.
In client side, either:
1. select column as is, convert using Double.parseDouble(ResultSet.getString(n)) and catch NumberFormatException for bad conversions.
or
2. select column as is, convert using internal driver conversion ResultSet.getDouble(n) and catch SQLException for bad conversions.
In server side, either use CAST or CONVERT field within the SELECT statement [http://dev.mysql.com/doc/refman/5.6/en/cast-functions.html#function_cast], then retrieve it with ResultSet.getDouble(n).
I'm using doubles in my suggestions but you should use the numeric type that's more appropriate to your case.
Please note that these two alternatives are not equivalent. Server side converting will always return a valid number, eventually 0 or 0.0 and can produce some warnings, while client side conversions throw exceptions in case of failure.
Subject
Written By
Posted
December 06, 2014 09:38PM
Re: How to check if a variable is numeric in MySQL
December 09, 2014 12:52PM
Sorry, you can't reply to this topic. It has been closed.
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.
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.