4

What would be a good way to select all the rows from a table where a column is an unsigned integer, for example here is some pseudo-sql

SELECT * FROM mytable WHERE mycolumn IS UNSIGNED INTEGER

So that strings 'abc' and numbers like '12.3' and '12.0' would not match, but integers like '123' would.

Where mycolumn is type text/varchar

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143

4 Answers4

9
SELECT * FROM mytable WHERE mycolumn REGEXP '^[0-9]+$'

Shouldn't that be simple enough?

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
2
select * from mytable  
where cast(mycolumn as int) = mycolumn 

Updated Example

EDIT

I did not see you have a varchar column. Try this instead:

select * from myTable 
where cast(cast(myColumn as decimal(8,2)) as signed) = 
      cast(myColumn as decimal(8,2))
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • I made a sample table mytable and tried it and got: `ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int) = mycolumn' at line 2` – Timo Huovinen Apr 27 '12 at 08:17
  • 1
    @YuriKolovsky: Do not write int in `'`. Use `as int` and not `as 'int'` – juergen d Apr 27 '12 at 08:20
  • @juergend the test should be with `myColumn varchar(30)`. THe OP has at char type column – Florin Ghita Apr 27 '12 at 08:21
  • now is perfect! +1 from me. However, the OP states in comments that values like 12.0 should be filtered out. IT's not what he initially asked – Florin Ghita Apr 27 '12 at 08:33
  • @juergend using your exact sql on a test table, where it's using `as int` and `as float` without the single quotes, still getting that error though. – Timo Huovinen Apr 27 '12 at 08:34
  • @YuriKolovsky: Please post the exact query you are trying and I will have a look. – juergen d Apr 27 '12 at 08:37
  • @juergend `select * from mytable where cast(cast(mycolumn as float) as int) = cast(mycolumn as float);` – Timo Huovinen Apr 27 '12 at 08:38
  • @juergend I tried it in sqlfiddle http://sqlfiddle.com/#!3/d2f21/1 and it worked, but fails to do so in my server, instead I get this error, maybe it has something to do with mysql? `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'float) as int) = cast(mycolumn as float)' at line 1` – Timo Huovinen Apr 27 '12 at 09:13
  • @YuriKolovsky: Yes it is about MySQL. I tried it accidently with SQL Server and not with MYSQL. I am trying to find an answer to the problem. – juergen d Apr 27 '12 at 09:17
  • @YuriKolovsky: I updated my answer. It works now but not with numbers like `12.0`. You should accept Robin's answer. – juergen d Apr 27 '12 at 11:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10594/discussion-between-yurikolovsky-and-juergen-d) – Timo Huovinen Apr 27 '12 at 11:24
0
SELECT * FROM mytable WHERE mycolumn = FLOOR(mycolumn)

//edit: OK, as question is specified, this answer no longer fits. It will reject 12.3 but match 12 AND 12.0 (what turned out to be not indended).

The revised problem can't be solved mathematically, only way is to match characters, as Robin Castlin did.

Agent_L
  • 4,960
  • 28
  • 30
0
create table test_int(
test_value varchar(5)
)

insert into test_int values('a')
insert into test_int values('1')
insert into test_int values('12.3')

select *
from test_int 
where isnumeric(
cast(test_value as varchar)) = 1

result

1
12.3
Diego
  • 34,802
  • 21
  • 91
  • 134