As usual, there are few "right" answers, only tradeoffs.
The direction to move to optimize this function depends on both your requirements and knowledge of the domain of input strings the function is to process.
For example, if a large proportion of the inputs are 100% numeric, and punctuation is not permitted, then a cheap way to optimize the execution is
create function bludata.onlyNumber(in val long varchar)
returns long varchar
begin
declare sTemp long varchar;
declare iCount integer;
if isnumeric(val) = 1 then return val endif;
set iCount=1;
set sTemp='';
while iCount <= length(val) loop
if substring(val,iCount,1) in( '0','1','2','3','4','5','6','7','8','9') then
set sTemp=sTemp+substring(val,iCount,1)
end if;
set iCount=iCount+1
end loop;
return sTemp
end
But the isnumeric() function only tests if conversion to a numeric is possible, not an integer - instead you can use a REGEXP search on the string to look for anything that is not the digits 0-9.
Another cheap test would be to attempt the conversion to a BIGINT if the input string is shorter than 19 characters (assuming a single-byte charset). Attempt the CAST, if it succeeds return val, otherwise process the string as you are.
Another possibility is to not consider the string character-by-character. If non-numeric characters are rare, use a substring search to find the next "chunk" of numbers; this will result in fewer concatentation operations, which will mean fewer memory allocations.