sqlserver按'一二三四五'排序(hibernate对中文数字进行排序)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gisboygogogo/article/details/78139162

当sql server数据表中某个字段值为 第一组,第二组,第三组...时,如果想对其进行排序,按照常规查询得到的结果如下:

select a.group_name, count(*) as cnt 
from tableA a 
group by a.group_name order by a.group_name asc

并不能得到“一二三四五”的顺序。

这时需要sql server里的两个函数,substring() 和 charindex(),SQL语句如下:

select a.group_name, count(*) as cnt, charindex(SUBSTRING(a.group_name,2,1),'一二三四五六七八九十') as place
from tableA a 
group by a.group_name order by PLACE

增加一个临时place 字段。

如果你需要hibernate和sqlserver集成,则需要把charindex函数换成LOCATE函数

select a.groupName, count(*) as cnt, LOCATE(SUBSTRING(a.groupName,2,1),'一二三四五六七八九十') as place from table a group by a.groupName order by place


猜你喜欢

转载自blog.csdn.net/gisboygogogo/article/details/78139162