PostgreSQL ignores dashes when ordering

你离开我真会死。 提交于 2019-12-05 15:29:35

This is because da_DK.utf8 locale defines it this way. Linux locale aware utilities, for example sort will also work like this.

Your convert_to(name, 'latin1') will break if it finds a character which is not in Latin 1 character set, for example , so it isn't a good workaround.

You can use order by convert_to(name, 'SQL_ASCII'), which will ignore locale defined sort and simply use byte values.


Ugly hack edit:

order by
  (
    ascii(name) between ascii('a') and ascii('z')
    or ascii(name) between ascii('A') and ascii('Z')
    or ascii(name)>127
  ),
  name;

This will sort first anything which starts with ASCII non-letter. This is very ugly, because sorting further in string would behave strange, but it can be good enough for you.

A workaround that will work in my specific case is to replace dashes with exclamation points. I happen to know that I will never get exclamation points and it will be sorted before any letters or digits.

select name from mytable order by translate(name, '-', '!') asc

It will certainly affect performance so I may look into creating a special column for sorting but I really don't like that either...

I don't know how seems ordering rules for Dutch, but for Polish special characters like space, dashes etc are not "counted" in sorting in most dictionaries. Some good sort routines do the same and ignores such special characters. Probably in Dutch there is similar rule, and this rule is implemented by Ubuntu locale aware sort function.

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