Teradata IsNumeric

Posted on 2006-11-13
Last Modified: 2012-06-21
Does anyone know a way to filter out non-numeric characters in a char field?

I am looking for something like the following that works with Teradata:
Select * from table where IsNumeric(field1) = 1

I have a problem converting a string to an integer on a field that "should" contain all numbers, but it doesn't. Or if there's any way to not get a blasted "Bad Character in format or data" error whenever I try to convert the field to an integer.
Question by:JeepGeekin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 65

Accepted Solution

rockiroads earned 500 total points
ID: 17933056
Have u considered creating a UDF and using that?
here is a premade one - look for is_integer.txt (2nd last post on page)

the code for this (in C) is

-- Function is_integer
--Call exampel: Select:select mydatabase.is_integer('123A4677')
--Result  : 1 when Integer
--          0 when not Integer
--SQL-registration in Teradata:

 SPECIFIC is_integer_v
 EXTERNAL NAME 'SS!is_integer_v!/home/mydatabase/udf/is_integer_v.c'

--Compilation in Unix: cc -c is_integer_v.c -o is_integer_v.o


#define SQL_TEXT Latin_Text
#include "sqltypes_td.h"
#include <string.h>
#include <stdlib.h>
#define IsNull -1
#define IsNotNull 0

void is_integer_v( VARCHAR_LATIN *inputString,
                   int *result,
                   int *inputStringIsNull,
                   int *ResultIsNull,
                   char sqlstate[6],
                   SQL_TEXT extname[129],
                   SQL_TEXT specific_name[129],
                   SQL_TEXT error_message[257] )
    char *ch;

    *error_message = '\0';
    *ResultIsNull = 0;

    /* make false result the default */
    *result = 0;

    /* Return Null value on Null Input */
    if ((*inputStringIsNull) == IsNull)

/* what is an Integer

         [+|-] xxxx

    ch = (char *)inputString;

    /* Skip leading spaces */
    /*   remove this while line to disallow Leading whitespace */
    while ( isspace(*ch))   ch ++;

    if ( (*ch == '-') || (*ch == '+' ))
        ch ++;

        /* is the first character at least a digit? */
    if ( !isdigit(*ch) ) return ;

    /* skip over all the digits */
    /* already validated first digit above */
    while (isdigit(*ch)) ;

    /* skip over the trailing white space */
    /* Remove this while line out   to disallow Trailing whitespace */
    while ( isspace(*ch))   ch ++;

    /* we should be at the end of the string.
       if not it is not an integer
    *result = ( *ch == '\0' );


the other method described is to use UPPER

Select * from table where field1 <> upper(field1)


Author Comment

ID: 17933266
Thanks! I don't have the ability to add UDFs to this system, but the Upper approach worked for all characters but the "~", "`", "@","#","$", ...
Then I just filter thi smuch smaler list out. Also, I had to add a lower statement & a casespecific statement:

select * from Upper(field) <> Lower(Field) (CASESPECIFIC)

Thanks again!

LVL 65

Expert Comment

ID: 17933310
Cool and thanks for posting your solution, be very useful for others in the same predicament


Best of luck with the rest of your project

Author Comment

ID: 17933514
OK. I feel stupid, so I'll post this as well. The Between statement can still recognize numeriic ranges when dealing with characters. This is probably the best way to handle this:

Select * from table where trim(field) BETWEEN '0' AND '99999'

(use as many 9's as you need)


Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question