PostgreSQL 9.1 – How to Fix Trim Not Working for Some Strings

postgresql-9.1string

I want to trim dot (.) from a column value. Just say string is "Mr." and I need to trim the dot from the string. I am expecting "Mr"

Lets say,

I have two fields in my table

 id   |  value
 1       Mr.
 2       Ms.
 11      Mr.
 12      Ms.

But while running below query, not getting as i expected.

 select trim(value, '.') from mytable

 value
 (text)
 Mr
 Ms
 Mr.
 Ms.

And noticed, for id 1 & 2 values is coming as expected. but for id 11 & 12 still the dot is coming. I really could not understand what is the issue.

(Using Postgresql 9.1)

Best Answer

I looked at the trim function and it removes the character from the beginning or the end of the string. I'd bet that the two where it's not working have spaces after the ".". Try trimming spaces off the end first then trim the "."

select trim(trim(value,' '), '.') from mytable