UserID UserName Password
1 abc 123
10 xyz 456
3 mno 254
SELECT MAX(UserId) AS UserId FROM UserLogin
When I run this query it gives me 3 instead of 10
All columns are TEXT datatype
Try to change
Your query is returning
3
because it is the larger value considering lexicographic order (anything starting with3
is considered greater than anything starting with1
, just like something starting withb
is greater than anything starting witha
).Use the
VAL
function to convert the TEXT columns into numeric values: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.
Make sure that the column type is numeric and not varchar or string.