Mindaugas Jakubauskas
Mindaugas Jakubauskas

Reputation: 439

Mysql numbers compare in varchar fields

I need to compare numbers stored in varchar fields, for example i have a table:

id | values
1 | 2
2| 154
3 | 88
4 | 35

and I need to look for numbers, that are higher than 5, if I could use int attribute for values fields everything would be ok, but I have to use varchar. Is there any simple solution?

Upvotes: 3

Views: 1671

Answers (1)

echo_Me
echo_Me

Reputation: 37243

you need to use

CAST(`values` AS UNSIGNED)

like that

select * from table WHERE CAST(`values` AS UNSIGNED) > 5

DEMO

EDIT:

lets say you have signed number with negative sign -

then

select * from table1 WHERE  CAST(`values` AS SIGNED) > 5

DEMO

Upvotes: 3

Related Questions