Select MAX INT value of TEXT column

三世轮回 提交于 2019-12-13 09:18:50

问题


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


回答1:


Your query is returning 3 because it is the larger 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.




回答2:


Make sure that the column type is numeric and not varchar or string.




回答3:


Try to change

SELECT MAX(val(UserId)) AS UserId FROM UserLogin


来源:https://stackoverflow.com/questions/16355592/select-max-int-value-of-text-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!