Hive: First and last occurrence in a string

Viewed 1507

I have an id column and a string column as follows:

id   values
1   AD123~DF123~SQ345
2   CF234~DF234
3   BG123

I need the first occurrence and last occurrence of columns below in Hive

id  first   last    
1   AD123   SQ345    
2   CF234   DF234    
3   BG123   BG123 

I have already tried using the HIVE split function to solve it

select id, split(values, '\~') [0] as first, reverse(split(reverse(values), '\~')[0]) from demo;

I keep getting a syntax error in Hive saying that [ is unexpected.

Another alternative I found is regex but I am new to Hive, can some one please help me out here with regex or split. Thanks

1 Answers

Using split:

with your_table as(
select stack(3,
1, 'AD123~DF123~SQ345',
2, 'CF234~DF234',
3, 'BG123'
 ) as (id,values)
 ) --use your_table instead of this

select id, values[0] as first, values[size(values)-1] as last
from
( 
 select id, split(values,'~') values
   from your_table t
)s   
;

Returns:

id      first   last
1       AD123   SQ345
2       CF234   DF234
3       BG123   BG123

Using regexp:

select id, 
       regexp_extract(values,'^([^~]*)',1) as first,
       regexp_extract(values,'([^~]*)$',1) as last
   from your_table t 
;
Related