Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql split_part - how to split after the first split key

given the text string

foo.bar.baz

how do I use split_part function of postgresql to get the rest after the first dot?

I want to extract from this text the next 2 texts:

foo
bar.baz

split_part requires number of part as argument so

split_part('foo.bar.baz', ''.', 2); 

returns

bar

but not bar.baz

how do I get this using this function, or any other available function?

Thanks

like image 252
eligro Avatar asked Sep 03 '25 03:09

eligro


1 Answers

I would probably use the regex form of substring:

substring('foo.bar.baz' from E'[^.]*\\.(.+)$')

This matches any number of non-. characters, then a dot, then any number of characters. Because the last part is in brackets it is captured and returned.

like image 137
harmic Avatar answered Sep 04 '25 21:09

harmic