principlesqlMinor
How does PostgreSQL compare strings?
Viewed 0 times
postgresqldoeshowcomparestrings
Problem
I'm running Postgresql 9.6 on Ubuntu 16.04. The collation order is en_ZA.UTF-8. I'm puzzled by how Postgres compares strings:
The response to the second query doesn't make sense to me.
test=> select 'b' > 'B';
?column?
----------
f
test=> select 'ba' > 'B';
?column?
----------
t
test=> select 'b' = 'B';
?column?
----------
f
test=> select 'ba' > 'C';
?column?
----------
fThe response to the second query doesn't make sense to me.
Solution
That's just how your locale defines sort order. Obviously upper case letters act as tiebreakers if the string is otherwise identical - then they sort after lower case equivalents. But 'ba' still sorts after 'B' (and 'BA' sorts after 'b').
Compare to results without collation rules:
(My current collation setting
Compare to results without collation rules:
SELECT 'b' > 'B'
, 'ba' > 'B'
, 'ba' > 'C'
, 'b' > 'B' COLLATE "C"
, 'ba' > 'B' COLLATE "C"
, 'ba' > 'C' COLLATE "C";?column? | ?column? | ?column? | ?column? | ?column? | ?column?
----------+----------+----------+----------+----------+----------
f | t | f | t | t | t(My current collation setting
German_Germany.1252 happens to behave just like your en_ZA.UTF-8.)Code Snippets
SELECT 'b' > 'B'
, 'ba' > 'B'
, 'ba' > 'C'
, 'b' > 'B' COLLATE "C"
, 'ba' > 'B' COLLATE "C"
, 'ba' > 'C' COLLATE "C";?column? | ?column? | ?column? | ?column? | ?column? | ?column?
----------+----------+----------+----------+----------+----------
f | t | f | t | t | tContext
StackExchange Database Administrators Q#165739, answer score: 2
Revisions (0)
No revisions yet.