How to use alphanumeric fields with BETWEEN clause in Mysql?

独自空忆成欢 提交于 2019-12-02 18:48:22

问题


I have a table that contain a field names as mgrs, the value that stored in mgrs fields is like '42SWC227821555' may contain more charachters, and may contain lower case letters. So now i want to search records between two mgrs, so how can i do that? can i convert mgrs value to integer first and then use in between clause?


回答1:


Instead of BETWEEN clause use STRCMP(expr1, expr2) function for string comparison operations:

WHERE STRCMP(mgrs, '42SWC227821555') >= 0 AND STRCMP(mgrs, '42SWC227821570') <= 0



回答2:


I will list some steps, instead of complete answer.

  1. Remove all alphabets from you value, means you can have 1 more customized column using function listed on this link
  2. Apply your filter on this column.



回答3:


You can use string expressions with BETWEEN comparison.

SELECT '42SWC2278215551' BETWEEN '42SWC227821555' AND '42SWd227821555'
-> 1


来源:https://stackoverflow.com/questions/25842771/how-to-use-alphanumeric-fields-with-between-clause-in-mysql

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