Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find not-integer values in a string column

How to select records which are having non-integer values in a particular column?

I tried like:

SELECT * FROM tableName WHERE status !~ '^\d+?\$'

I want to find all records not storing exact integer representations.

  Column   |         Type           | Modifiers 
------------+-----------------------+-----------
 status    | charecter varying(25)  |
like image 526
Baraskar Sandeep Avatar asked Sep 13 '25 21:09

Baraskar Sandeep


2 Answers

Since you're looking for non-integer values, if the status contains anything that isn't a digit (i.e. a letter, decimal point, etc.), it's not an integer, so this regex should work:

select * from foo where status ~ E'[^\\d]'

Note the double-escape of the backslash and the use of the negated character class.

Here's an sqlfiddle.

like image 185
khampson Avatar answered Sep 17 '25 00:09

khampson


Postgres uses signed integer, we need to allow an optional leading minus or plus sign (+-). Not valid as integer literals:

SELECT * FROM tbl WHERE status !~ '^[+-]*\d+$'

Note that leading and trailing whitespace is tolerated and trimmed, but not nested whitespace.

fiddle
Old sqlfiddle.

like image 29
Erwin Brandstetter Avatar answered Sep 16 '25 23:09

Erwin Brandstetter