Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

joining two tables using SUBSTR - response (1) by M.Saeed Khurram

$
0
0

Hi,
The following thread will help you to construct a solution, you can get and insert these values into a temp table and then join on the basis of these values.
http://forums.teradata.com/forum/enterprise/split-delimited-column-into-separate-rows
I have modified this solution for your scenario:

CREATE TABLE td3471.Test_x
(
Delimited_String VARCHAR(100)
)

INSERT INTO td3471.Test_x
VALUES ('FL,CHG,MN');
INSERT INTO td3471.Test_x
VALUES ( 'MN,MN');
INSERT INTO td3471.Test_x
VALUES( 'DET');


WITH RECURSIVE parse_list (response_key, delim_pos, item_num, element, remainder) AS
(
        SELECT
        SUBSTRING( Delimited_String FROM 1 FOR POSITION(',' IN Delimited_String)) AS response_key
        ,0, 0, CAST(response_key AS VARCHAR(100)) AS element
        ,SUBSTRING(Delimited_String FROM POSITION(',' IN Delimited_String)+1 FOR CHAR_LENGTH(Delimited_String)) AS remainder
        FROM
        TD3471.Test_x
UNION ALL
    SELECT response_key,
           CASE WHEN POSITION(',' IN remainder) > 0
             THEN POSITION(',' IN remainder)
             ELSE CHARACTER_LENGTH(remainder) END dpos,
           item_num + 1,
           TRIM(BOTH ',' FROM SUBSTR(remainder, 0, dpos+1)),
           TRIM(SUBSTR(remainder, dpos+1))
    FROM   parse_list
    WHERE  dpos > 0
)
SELECT DISTINCT TRIM(BOTH ',' FROM element)
FROM   parse_list p
WHERE element NOT LIKE ''
ORDER BY response_key, item_num;

 


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>