简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM