sqlreplaceleft-jointeradatadirty-data

Teradata: How to LEFT OUTER JOIN unlike datatypes?


SELECT DR.column
FROM DB.Table RCT
LEFT OUTER JOIN DB.Table2 RC ON RCT.NBR_CLMN = RC.NBR_CLMN
LEFT OUTER JOIN DB.Table3 DR ON DR.NEW_NBR_CLMN = RC.VCHAR_COLUMN

Running the SQL code above in Teradata SQL Assistant gives me a 2620 "The format or data contains a bad character" error. After tracking down the exact cause of the problem, it seems to be due to DR.NEW_NBR_CLMN and RC.VCHAR_COLUMN being different datatypes with data such that the implicit conversion is failing.

I've tried using CAST AS VCHAR and using CAST AS BIGINT on both sides of the join like so:

LEFT OUTER JOIN DB.Table3 DR ON CAST(DR.NEW_NBR_CLMN AS BIGINT) = CAST(RC.VCHAR_COLUMN AS BIGINT)

Unfortunately, casting as vchar returns null values from the DR table as nothing matches. Casting as bigint returns the same error as before. Part of the problem is that one column contains commas in the number while the other does (ex. "66993001968" and "66,993,001,968").

So I tried using REPLACE to remove the commas, but Teradata tells me that it isn't correct usage. More specifically, this error: "SELECT Failed. [3706] Syntax error: expected something between the 'ON' keyword and the 'REPLACE' keyword."

Here is what I am trying:

LEFT OUTER JOIN DB.Table2 RC ON REPLACE(DR.NEW_NBR_CLMN, ',', '') = RC.VCHAR_COLUMN

Any help with the overall join problem or with the more specific REPLACE problem would be greatly appreciated!

Example Data:

DR Table Example: DR Table Example

RC Table Example:

RC Table Example

Desired Result: Desired Result


Solution

  • You can use regexp_replace() to remove all non-digits:

    ON DR.NEW_NBR_CLMN = cast(regexp_replace(RC.VCHAR_COLUMN, '[^0-9]', '') as bigint)