Solved

Teradata IsNumeric

Posted on 2006-11-13
4
9,663 Views
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.
0
Comment
Question by:JeepGeekin
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
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)
http://www.teradata.com/teradataForum/shwmessage.aspx?ForumID=5&MessageID=4532

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:

REPLACE FUNCTION mydatabase.IS_INTEGER (myval VARCHAR(64000) CHARACTER SET LATIN)
 RETURNS INTEGER
 SPECIFIC is_integer_v
 LANGUAGE C
 NO SQL
 PARAMETER STYLE SQL
 DETERMINISTIC
 CALLED ON NULL INPUT
 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)
    {
        return;
    }

/* 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 */
    do
    {
        ch++;
    }
    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' );

    return;
}





the other method described is to use UPPER

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

0
 

Author Comment

by:JeepGeekin
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!

0
 
LVL 65

Expert Comment

by:rockiroads
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
0
 

Author Comment

by:JeepGeekin
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)


0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

856 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