12

I want to convert a column of type "character varying" that has integers with commas to a regular integer column.

I want to support numbers from '1' to '10,000,000'.

I've tried to use: to_number(fieldname, '999G999G999'), but it only works if the format matches the exact length of the string.

Is there a way to do this that supports from '1' to '10,000,000'?

1
  • I use this function for parsing integer from an arbitrary string. Commented Apr 26, 2016 at 8:24

4 Answers 4

29
select replace(fieldname,',','')::numeric ;

To do it the way you originally attempted, which is not advised:

select to_number( fieldname,
                  regexp_replace( replace(fieldname,',','G') , '[0-9]' ,'9','g')
                );

The inner replace changes commas to G. The outer replace changes numbers to 9. This does not factor in decimal or negative numbers.

Sign up to request clarification or add additional context in comments.

3 Comments

Replacing the comma is smart, easy, and probably a lot faster than using to_number(). Thanks.
There is a way using to_number (in case you're interested).. Please look my answer
@vol7ron I have a query. When an application/website works with different regions, users try to insert values based on their local standards. Germans use dots as thousand separators and commas as decimal separators while many regions use them inversely. Now, my question is, what is the format of PostgreSQL to store numbers with a decimal's and thousand's separators?
1

You can just strip out the commas with the REPLACE() function:

CREATE TABLE Foo
(
  Test NUMERIC
);

insert into Foo VALUES (REPLACE('1,234,567', ',', '')::numeric);

select * from Foo; -- Will show 1234567

Comments

0

You can replace the commas by an empty string as suggested, or you could use to_number with the FM prefix, so the query would look like this:

SELECT to_number(my_column, 'FM99G999G999')

4 Comments

select to_number('1,000,000,065','FM99G999G999'); results in 100000 (and so does 1,000,000,000 and 10,000,000). FM is a fill mode; which only suppresses padding blanks and trailing 0s
@vol7ron It still works if you use a format to admit larger numbers, e.g 'FM999G999G999G999G999'. Still, it looks like to_number is a little buggy, because it does not work with '9G999G999G999'
yeah check here , which has enough placeholders but drops the last digit (odd)
Here's the same example in 9.6, seems like the issue still exists (sqlfiddle.com/#!17/538b7/1) I never did submit a bug report
-1

There are things to take note:

When using function REPLACE("fieldName", ',', '') on a table, if there are VIEW using the TABLE, that function will not work properly. You must drop the view to use it.

1 Comment

This is important, but it should be a comment to one of the other answers. By itself, this does not answer the question independently but instead seems to reference previous answers.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.