TERADATA - How to split a character column and keep the last token?

2.4k views Asked by At

I have a table with article names and I would like to select the last word of each article of the table.

Right now I'm doing it in SAS and I my code looks like:

PROC SQL;
    CREATE TABLE last_word as
    SELECT scan(names,-1) as last_w
    FROM articles;
QUIT;

I am aware of the STRTOK function in TERADATA but it seems that it only accepts positive values as indexes and in my case the articles names don't have a constant number of words.

1

There are 1 answers

0
JNevill On BEST ANSWER

You could use function REGEXP_SUBSTR to do this:

CREATE TABLE last_word as
SELECT REGEXP_SUBSTR(names, '[^,]+$') as last_w
FROM articles;

The Regex here will grab the last element of the list, where the list is comma delimited.