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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL: I need to add an index on a field 5 50
EF5 How do I stop pre-compiled views? 8 54
User Prompt in Vertica SQL (vsql) 1 39
Best way to get data into a database 12 56
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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