4

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)  |
0

2 Answers 2

7

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.

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

1 Comment

You don't need the double backslash if you use a standard SQL character literal: '[^\d]'
5

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.

Comments

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.