Jip Harthoorn
Jip Harthoorn

Reputation: 302

SQL compare integers from column, output is wrong

I have a SQL table on PHPMyAdmin with two columns that both contains integers (see image)

https://i.sstatic.net/Shcsl.png

When the row is empty, the value is 'null'. If I execute the following query:

SELECT * FROM `18-3-2019` WHERE `COL 13` < `COL 14`

I get the following result (left column is COL 13, right one is COL 14):

https://i.sstatic.net/PgMmy.png

The problem is that the statement isn't always true, for example the output 12 being smaller than 6. I see the relation between numbers higher then 10 being in the output as they are lower than the other number. How do I fix this?

Upvotes: 0

Views: 440

Answers (1)

Sebastian Brosch
Sebastian Brosch

Reputation: 43574

You need to CAST the string values (VARCHAR) to integers (INT / SIGNED).

SELECT '12' < '6'; -- 1

using CAST(column AS SIGNED) to compare the values:

SELECT 12 < 6; -- 0
SELECT CAST('12' AS SIGNED) < CAST('6' AS SIGNED); -- 0

demo on dbfiddle.uk

Upvotes: 1

Related Questions