This fixes the problem of negative numeric values. It does not however
handle locale specific numbers (like using , as decimal separator) or for
that matter, commas in the numbers. As I understand it, sqlite does not
handle locale specific numbers in general.
Mike
/*
** Implementation of the isnumeric() function
*/
static void isnumericFunc(sqlite3_context *context, int argc, sqlite3_value
**argv)
{
int i;
int nResult = 1;
assert( argc==1 );
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_INTEGER: {
sqlite3_result_int(context, 1);
break;
}
case SQLITE_FLOAT: {
sqlite3_result_int(context, 1);
break;
}
case SQLITE_NULL: {
sqlite3_result_int(context, 0);
break;
}
case SQLITE_TEXT: {
int d = 0;
const char *z = sqlite3_value_text(argv[0]);
for (i = 0; i < strlen (z); i++) {
if (!isdigit (z[i])) {
/* the character is not a digit */
if (i == 0) {
/* allow - or + as the first character */
if ((z[i] != '-') && (z[i] != '+') && (z[i] != '.')) {
/* only +, - and . allowed as first non digit character */
nResult = 0;
if (z[i] == '.')
d++;
break;
}
}
else {
if ((d > 0) && (z[i] == '.')) {
/* only . allowed as non digit character here */
/* and only one of them in the string */
nResult = 0;
break;
}
else if (z[i] == '.')
d++;
}
}
}
sqlite3_result_int(context, nResult);
break;
}
default: {
sqlite3_result_int(context, 0);
break;
}
}
}
> -----Original Message-----
> From: Wolfgang Rohdewald [mailto:[EMAIL PROTECTED]
> Sent: Saturday, May 14, 2005 8:39 AM
> To: [email protected]
> Subject: Re: [sqlite] Convert and isnumeric function
>
> On Samstag 14 Mai 2005 00:31, Michael Evenson wrote:
> > case SQLITE_TEXT: {
> > const char *z = sqlite3_value_text(argv[0]);
> > for (i = 0; i < strlen (z); i++) {
> > if (!isdigit (z[i])) {
> > nResult = 0;
> > break;
> > }
>
> this should return FALSE for -5, 1123.456.
> In some locales, that might be written as 1'123,456 or
> 1,123.456 or whatever.
>
> strtol()
>
> seems to what you need, see man strol()
>
> --
> Wolfgang
>