As some values in column phone exceeds the max allowed limit for INTEGER i.e. 2147483647, so i assume that BIGINT is the datatype. In this case below is the query that will return your desired result.
SELECT DISTINCT t1.Name,
t1.Phone,
t2.areacode
FROM t1
LEFT JOIN t2 ON substring(t1.Phone
FROM 11
FOR 3) = t2.areacode
ORDER BY 3 DESC;
substring impliticitly cast bigint to 20 space variable character left justified as bigint requires 20 character, so the starting point for substring is 11. Also teradata implicitly compare character with INT, so no need to cast areacode
Other option is to use trim before substring to remove leading spaces as below.
SELECT DISTINCT t1.Name,
t1.Phone,
t2.areacode
FROM t1
LEFT JOIN t2 ON substring(trim(t1.Phone)
FROM 1
FOR 3) = t2.areacode
ORDER BY 3 DESC;
DISTINCT is used to avoid duplicates, as area code has duplicate values, so joining it to the phone record table will create duplicate rows.
Result:
Name Phone areacode
----------------------------------
Jack 2,221,231,234 222
Jenny 2,224,321,234 222
Joe 1,111,231,235 111
John 1,111,231,234 111
Jody 3,323,214,211 ?
P.S. As substring and trim functions are same in Teradata and MySQL, you can check the demo here
Hope this will help :-)