Oracle order by different
-
27-10-2019 - |
Question
I have a table in an Oracle Database like this
ID | LABEL
------------
1 | label alpha 1
2 | label alpha 2
3 | label alpha a
when I do a select in an application like Squirrel like this :
select * FROM MA_TABLE order by LABEL asc
I get :
ID | LABEL
------------
1 | label alpha 1
2 | label alpha 2
3 | label alpha a
which is fine !
but When I execute the same request using MyBatis :
<select id="selectMaTable" resultMap="resultMap" >
Select * FROM MA_TABLE order by LABEL asc
</select>
I get :
ID | LABEL
------------
3 | label alpha a
1 | label alpha 1
2 | label alpha 2
The alphabetic characters come before the numeric characters... why ??
thanks in advance,
Antoine
Ps : I am using org.mybatis:mybatis:jar:3.0.5 and com.oracle:ojdbc6:jar:11.2.0.2.0 for database access
Edit : this linked help me a bit also
Thanks to Soulcheck's remark, I found that if I change the order by clause with ORDER BY NLSSORT(ATL_SIT.ATL_SIT_LIB, 'NLS_SORT=BINARY') it works...
Does anyone knows how to force NLS_SORT=BINARY with myBatis ? (It is already set on my Oracle database in NLS_DATABASE_PARAMETERS)
Solution
It looks like it might be a locale issue. Connect with squirrel and check what NLS_LANG
it sets, then check what locale uses your java app. Another parameter that can influence sort is NLS_SORT
.
You can check the value of both by issuing:
select parameter,value from NLS_DATABASE_PARAMETERS where parameter in('NLS_LANGUAGE','NLS_SORT');
Then you can test it in java by using:
Locale.getDefault()
and set it by using:
Locale.setDefault(Locale)
or by adding jvm parameters:
-Duser.country=en -Duser.language=en
edit
spring forums recommend creating a logon trigger which sets NLS_SORT environmental variable on user logon. It's not MyBatis, but jdbc anyway so should work in your case.