Search code examples
sqlprestosql-function

How can i cut the left part of the string with unknown legth? (with sql function)


In the ETL process, I receive a varchar field, and the length (of the value) is changed from row to row. I need to keep 5 symbols from the right side of the string. It means that I need to cut the left side but I can't, due to the unknown length.

I've tried the select substring('24:15:11',4, 5), but it doesn't help me, the string could be '2019-05-01 22:15:11'.

sql:

select substring('24:15:11',4, 5)

expected:

15:11

Solution

  • You can use substr. Negative starting position is interpreted as being relative to the end of the string.

    select substr('24:15:11', -5)