Your query is returning 3
because it is the largest value considering lexicographic order (anything starting with 3
is considered greater than anything starting with 1
, just like something starting with b
is greater than anything starting with a
).
Use the VAL
function to convert the TEXT columns into numeric values:
SELECT MAX(VAL(UserId)) AS UserId FROM UserLogin
If you're concerned about performance, you should make this column numeric, though. Take into account you're calling a function for every row in the table. Also, it won't be using any indexes this column may have.