Niklas
Niklas

Reputation: 13145

Is there an sql condition that can look for non integers in a column?

Basically I would like a select statement that would function like this

SELECT *
FROM table  
WHERE column IS NOT INT  

Is there a condition like this or how do you check for non-integers in an nvarchar(10) column?

Upvotes: 23

Views: 50542

Answers (3)

Anathi Matshaya
Anathi Matshaya

Reputation: 41

This is so weird

SELECT * FROM TABLE WHERE Period_Year > 1900 AND Period_Month BETWEEN 1 AND 12

Fixed the problem.

the database has no value that are less or eqeul to 1900 and it has no months NOT BETWEEN 1 OR 12.

I also did a SELECT DISTINCT ISDATE(CONCAT( Period_Year,'/',Period_Month,'/', 1)) FROM TABLE

I GOT A TRUE i.e 1 BACK

NO FALSE i.e 0 WHERE RETURNED

only 1 row with 1 for true

Even when I do SELECT * FROM TABLE WHERE Period_Year > 1

this fixes the problem

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425693

In SQL Server you can do:

SELECT  *
FROM    mytable  
WHERE   CASE WHEN IsNumeric(mycolumn) = 1 THEN CASE WHEN CAST(mycolumn AS FLOAT) <> CAST(CAST(mycolumn AS FLOAT) AS INT) THEN 1 END ELSE 1 END = 1

Upvotes: 17

Martin Smith
Martin Smith

Reputation: 453677

You could also use

SELECT  *
FROM    T 
WHERE  C = ''
        OR C LIKE '%[^0-9-]%' /*Contains a char other than - or 0-9*/
        OR C LIKE '_%-%'  /*Contains the - char other than 1st position*/

Upvotes: 16

Related Questions