HiveBrain v1.2.0
Get Started
← Back to all entries
principlesqlMinor

How does PostgreSQL compare strings?

Submitted by: @import:stackexchange-dba··
0
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:

test=> select 'b' > 'B';
 ?column? 
----------
  f

test=> select 'ba' > 'B';
?column? 
----------
  t

test=> select 'b' = 'B';
?column? 
----------
  f

test=> select 'ba' > 'C';
 ?column?  
----------
 f


The 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:

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        | t

Context

StackExchange Database Administrators Q#165739, answer score: 2

Revisions (0)

No revisions yet.