20

I need to remove non-numeric characters in a column (character varying) and keep numeric values in postgresql 9.3.5.

Examples:

1) "ggg" => ""
2) "3,0 kg" => "3,0"
3) "15 kg." => "15"
4) ...

There are a few problems, some values are like:

1) "2x3,25" 
2) "96+109" 
3) ...

These need to remain as is (i.e when containing non-numeric characters between numeric characters - do nothing).

2 Answers 2

66

Using regexp_replace is more simple:

# select regexp_replace('test1234test45abc', '[^0-9]+', '', 'g');
 regexp_replace 
----------------
 123445
(1 row)

The ^ means not, so any character that is not in the range 0-9 will be replaced with an empty string, ''.

The 'g' is a flag that means all matches will be replaced, not just the first match.

3
  • OK, regexp_replace looks a bit more easy, but how to keep values like "2x3,25" and "96+109" @pensnarik
    – mihk3l
    Feb 19, 2015 at 14:29
  • but you need to allow the decimal like 34.233 or the value changes completely is there possibility to allow the decimal point ? Jul 7, 2017 at 11:50
  • 1
    @AbhijitGujar Just use "[^0-9\.]+" as your regex query. that will match any character that is not 0-9 or '.'. Jul 26, 2017 at 17:00
12

For modifying strings in PostgreSQL take a look at The String functions and operators section of the documentation. Function substring(string from pattern) uses POSIX regular expressions for pattern matching and works well for removing different characters from your string.
(Note that the VALUES clause inside the parentheses is just to provide the example material and you can replace it any SELECT statement or table that provides the data):

SELECT substring(column1 from '(([0-9]+.*)*[0-9]+)'), column1 FROM
    (VALUES
        ('ggg'),
        ('3,0 kg'),
        ('15 kg.'),
        ('2x3,25'),
        ('96+109')
    ) strings

The regular expression explained in parts:

  • [0-9]+ - string has at least one number, example: '789'
  • [0-9]+.* - string has at least one number followed by something, example: '12smth'
  • ([0-9]+.\*)* - the string similar to the previous line zero or more times, example: '12smth22smth'
  • (([0-9]+.\*)*[0-9]+) - the string from the previous line zero or more times and at least one number at the end, example: '12smth22smth345'
2
  • what's the difference between [0-9]+.* VS. [0-9]+.* or ([0-9]+.*)* VS. ([0-9]+.*)*? Test some examples but couldnt find how the back slash plays differently here
    – uniquegino
    May 22, 2018 at 18:48
  • I think the backslash here is redundant (maybe harmful). Point '.' is any character and asterisk '*' means that character zero or more times. So '.*' means any char zero or more times. Backslash is usually reserved for escaping so that .\* (point backslash asterisk) means any character followed by asterisk. Can't test on postgresql as I don't have a server currently installed. Those backslashes might have appeared because when editing text in stackoverflow they need escaping too. For example in your comment the backslashes have disappeared. May 23, 2018 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.