字符串排序问题(http://sdusun.itpub.net/post/7915/200520)
[@more@]
如果排序的字段为字符串的时候,会出现问题,例如id为char或者Varchar型,select id from t_test order by id,会出现下列情况:
id
1
12
13
2
这个跟预想的结果不一样,这个时候需要将char变成int型,sql提供了一个通用的类型转换方法CAST() 。
CAST() 函数语法如下
CAST() ( AS [ length ])
上面语句可以这样写
select id from t_ test order by CAST( id as int)
得到结果:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/188692/viewspace-905862/
,如需转载,请注明出处,否则将追究法律责任。