sqlamazon-athenaprestotrino

AWS Athena: Right function


I would like to use the function right from AWS athena, but it does not seem to be supported.

How would I go about and trimming certain characters in Athena?

For example I would like to do RIGHT('1313521521', 4)

to get 1521. Unfortunately I would get something like

Queries of this type are not supported

Solution

  • Athena uses Presto as SQL engine and it does not have right function, but you can mimic it using substr and determining the staring position greatest(length(str) - 3, 1) - we need to start from 4th from last index, if string is too short - start from 1st index, cause Presto indexes starting from 1):

    --sample data
    with dataset(str) as (
        VALUES ('id1'),
        ('1313521521'),
        ('')
    )
    
    -- query
    select substr(str, greatest(length(str) - 3, 1))
    from dataset
    

    Output:

    _col0
    id1
    1521