Home > front end >  Split string and not take right of last element Presto
Split string and not take right of last element Presto

Time:06-17

If I have a table with a value of:

Hello/Hi/Howdy/Bye

How can I extract the field by just capturing

Hello/Hi/Howdy

Using SQL, specifically Presto. I don't fully understand how to implement the reverse of answers to the question here: Split string and take last element

SELECT reverse(split_part(reverse(column_name), '/', 1))

For instance, the above would take the string, reverse it, split the first part, then reverses it to get the last part. Is there a way to capture the index of the last instance of '/' to implement into split_part() to do something like this:

split_part(rcolumn_name, '/', < index of last instance of '/' >)

CodePudding user response:

If the goal is to remove the last / and everything after that - you can try using regexp_replace:

 select regexp_replace('Hello/Hi/Howdy/Bye', '(\/[^\/]*$)', '')

Output:

_col0
Hello/Hi/Howdy
  • Related