0

I have a problem to convert column with text values into integer values.

In table address i have column 'postcode' type TEXT. I have created a new column name 'postcode_int' type integer. In column 'postcode' some people wrote for example '330...' or '00234'. How can i check if the value is an integer.And if a value is an Integer then how to convert this values from type TEXT into type Integer and set them in column 'postcode_int' so that later i can do 'between '11111' and '99999'.

i tried to do something like this:

UPDATE adresse 
   SET postcode_int= case 
  when pg_typeof( REGEXP_REPLACE( COALESCE(trim( LEADING '0' FROM postcode), '0'), 
                                 '[^0-9]*' ,'0')::integer
                ) = integer 
  then postcode_int
  else 0 
   end;

With regards

Andrey

Andrew Ramnikov
  • 783
  • 2
  • 9
  • 30

1 Answers1

0

I believe the SQL you need is:

UPDATE adresse 
SET postcode_int = CAST(postcode AS INT)

This is based on your example.

If this answer is not clear please ask for a revision.

A. Greensmith
  • 355
  • 1
  • 8