How to escape underscore '_' in PostgreSQL trim() function?
I try to remove 'vt_' from begining of text (for example 'vt_test' ) using trim() function:
select trim(leading 'vt_' from 'vt_test');
select ltrim('vt_test', 'vt_');
select ltrim('vt_test', 'vt\_');
select ltrim('vt_test', 'vt\\_');
Returns:
est
But I would like to get:
test
I can do that using replace() but I would like to know why trim() doesn't work.
Tested on Postgres 12 and 11.
SUMMARY
- Function
trimremoves all leading instances of the listed characters - the order of the characters doesn't matter. You get the same result usingtrim(leading '_tv' from 'vt_test'). - I think, that the best solution is to use
select regexp_replace('vt_test', '^vt_', '')because I only want to remove this leading string only ifvt_exists at the begining (I'm sorry, but I didn't mention it before).
Thanks a_horse_with_no_name, Mureinik and Erwin Brandstetter for help!