开发者

Sort table by integer value stored in varchar field

Me working in spring hibernate

I have column in my database table, that of type VARCHAR, but it store integer value. So if i sort it using开发者_如何转开发 sql or hql or Criteria(Order.asc), all are sorting it as string. I need it to be sorted as integer value it store. Here i cannot alter my table.

Is There anyway to sort it as integer using Criteria

Is the only solution for me is, after reading it to some list and sort inside my service?

Edited : me using MYSQL

Thank you


You could try casting it before sorting it. In mysql, something like this:

SELECT CAST(myVarcharField AS DECIMAL(10)) as myIntField order by myIntField; 

or as an unsigned integer:

SELECT CAST(myVarcharField AS UNSIGNED) as myIntField order by myIntField; 

cast appears to be a valid HQL expression too.


SQL:

SELECT CAST(t.order AS UNSIGNED INTEGER) AS intOrder 
    FROM myTable t ORDER BY intOrder ASC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜