Re: Checking datatype of CHAR variable in 4GL
Posted in 1998
Ben Knox wrote: > > Does anyone if there a function in Informix 4gl (version 4) which will let > you check the contents of a CHAR variable to see whether or not it is a > valid number, without causing a numeric conversion error? > > I have an application which is requesting input from the user, and the input > can be a number, or a string. This then gets put through a CASE statement, > more or less as follows: > > DEFINE > var CHAR(5) > > PROMPT "Enter data: " FOR var > > CASE > > WHEN var>=1 AND var<=9999 > DISPLAY "Var is a number" > WHEN var IS NOT NULL > DISPLAY "Var is a string" > END CASE > > The problem I've got is that if the user enters a string, the program bombs > out with a "Character to numeric conversion error" at the first WHEN clause. Write a 4GL callable "C" wrapper function for the "C" atol() function or strtol() function which will return "0" for a string that does not start with a digit. Or just check (var[1,1] < "0" OR var[1,1] > "9"). The latter will not catch a string that starts with digits but contains non-numerics. You could have the "C" function perform both checks to be certain. It can just return TRUE or FALSE and you can use the CASE on that result Art Kagel > Lastly, does anyone know if there is a "catchall" WHEN statement (like the * > in a UNIX shell CASE statement). Yes the OTHERWISE clause.