redteel
redteel

Reputation: 3

Trim all occurences of a character in PostgreSql

I am trying to remove all the zeros from the values of the attribute salary. I am using TRIM function (by typecasting salary into TEXT). TRIM function does not remove 0s that are in between, for example, if I use it on 809800 the result is '8098'. The '0' in the middle is still there. Here is what I am doing:

PostgreSql

Thanks in advance.

Upvotes: 0

Views: 719

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269523

You can use translate() as well:

select translate(salary, '0', '')

For one character, this is just like replace(). However, this generalizes more nicely to multiple characters. You can just list the characters instead of nesting calls to replace().

Upvotes: 0

S-Man
S-Man

Reputation: 23676

Use replace() for that:

demo:db<>fiddle

SELECT replace('01000200330004000', '0', '')

or for using with your salary column:

SELECT replace(salary, '0', '')

Upvotes: 1

Related Questions