开发者

How to get the closest character that is greater than the given one in postgresql?

开发者 https://www.devze.com 2023-04-11 23:24 出处:网络
I n开发者_开发知识库eed to get the closest character specific to locale that is greater than (in terms of string comparison) the given one in postgresql. I tried to use

I n开发者_开发知识库eed to get the closest character specific to locale that is greater than (in terms of string comparison) the given one in postgresql. I tried to use

SELECT chr(ascii(x)+1);

But when I test it, it doesn't work sometimes in the way I want, for

SELECT chr(ascii('я')+1);

returns ѐ, but

SELECT 'я' < 'ѐ';

returns FALSE.


Sort order of text depends on lc_collate, not on the ASCII code or Unicode code point. These happen to go hand in hand with basic ASCII characters in most locales. The rest may vary.

What's your output of show lc_collate?

The behaviour you are expecting only works with locale C. Read all about it in the fine manual:

The C and POSIX collations both specify "traditional C" behavior, in which only the ASCII letters "A" through "Z" are treated as letters, and sorting is done strictly by character code byte values.

Emphasis mine. PostgreSQL 9.1 has a couple of new features for collation.


In locales other than C/POSIX, this is in general not possible without an exhaustive search. You had better reconsider your requirement.

If you are only dealing with a limited problem space in practice, such as Russian or Latin alphabets, I suggest you put an explicit letter list somewhere in your application. But if you need this working for arbitrary Unicode characters or strings, you're going to have some issues.

0

精彩评论

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

关注公众号