views:

36

answers:

2

hello all,

I have a teradata table with about 10 million records in it, that stores a numeric id field as a varchar. i need to transfer the values in this field to a bigint column in another table, but i can't simply say cast(id_field as bigint) because i get an invalid character error. looking through the values, i find that there could be a character at any position in the string, so let's say the string is varchar(18) i could filter out invalid rows like so :

     where substr(id_field,1,1) not in (/*big,ugly array of non-numeric chars*/)
     and substr(id_field,2,1) not in (/*big,ugly array of non-numeric chars*/)

etc, etc... 

then the cast would work, but this is not feasible in the long run. it's slow and if the string has 18 possible characters, it makes the query unreadable. how can i filter out rows that have a value in this field that will not cast as a bigint without checking each character individually for an array of non-numeric characters?

example values would be

   123abc464
   a2.3v65
   a_356087
   ........
   000000000
   BOB KNIGHT
   1235468099

the values follow no specific patterns, I simply need to filter out the ones that contain ANY non-numeric data. 123456789 is okay but 123.abc_c3865 is not...

thanks for your help in advance :)

-C

A: 

Please provide some examples.

Do you mean you have data like this?

123abc456

Then do you call it numeric id field?

Wei
what does your last question mean? i've added examples, but it's a pretty straightforward question... if you need someone to expand on a question, ask them to do so in the comments, this is not an answer.
Chris Drappier
A: 

The best that I've ever managed is this:

where char2hexint(upper(id_field)) = char2hexint(lower(id_field))

Since upper case characters give a different hex value to lower case ones, this will ensure that you have no alphabetical characters, but will still leave you with underscores, colons and so forth. If this doesn't meet your requirements, you may need to write an UDF.

lins314159
I do have to deal with special characters, unfortunately. what is an UDF?
Chris Drappier
User defined function, written in C. There's one for checking BigInts at http://developer.teradata.com/blog/madmac/2010/03/a-few-basic-scalar-string-udfs
lins314159