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
4

2 Answers

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

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

You can use length() to determine the 2nd argument of substr():

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

or simply:

select substr('24:15:11', length('24:15:11') - 4)

Read about preosto's string functions.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest