8

I have a table which contains data gathered from meteorological observations. The data where ill prepared so they are mistakes when they were inserted to the database. In a recent browse i've found non-numerical data where they should't be any.

How I can select the rows where non-numerical values exist?

The table's layout is as follows:

#\d data.tempe
               Table "data.tempe"
   Column   |         Type          | Modifiers 
------------+-----------------------+-----------
 id-station | integer               | 
 year       | integer               | 
 t1         | character varying(25) | 
 t2         | character varying(25) | 
 t3         | character varying(25) | 
 t4         | character varying(25) | 
 t5         | character varying(25) | 
 t6         | character varying(25) | 
 t7         | character varying(25) | 
 t8         | character varying(25) | 
 t9         | character varying(25) | 
 t10        | character varying(25) | 
 t11        | character varying(25) | 
 t12        | character varying(25) |  
1
  • or another solution would be to change non-numeric values to a predefined value Commented Jul 18, 2012 at 22:22

2 Answers 2

13
WHERE field !~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$'
Sign up to request clarification or add additional context in comments.

Comments

6

From this message on the PostgreSQL mailing list, you should be able to use a regular expression.

For example, something like this should nullify the invalid values in t1:

UPDATE data.tempe SET t1=NULL
    WHERE t1 !~ E'^[+-]?[0-9]+(\\\\.[0-9]*)?([Ee][+-]?[0-9]+)?\$';

(The regular expression may depend on the data format you expect.)

3 Comments

hmm regedit? how about then for UPDATE data.tempe SET t1=NULL WHERE t1 !~ E'[^0-9]';
What do you mean "regedit"? No, E'[^0-9] would exclude anything that contains a number. E'^[0-9]+\$' perhaps for positive integers.
heh sorry a bit tired, I meant "regexp". Your original suggestion along with a bit of tinkering should do the trick. I'll try it tomorrow and I'll post the results!

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.