What makes you think you are comparing dates?
Actually, you are comparing string literals which - in the absence of a cast context and any explicit cast - default to text
:
SELECT '20150526' > '2015-05-26' AS text2text
, '20150526'::date > '2015-05-26'::date AS date2date;
text2text | date2date
----------+----------
t | f
Try:
SELECT pg_typeof(col) FROM (VALUES ('20150527')) t(col); -- no cast
pg_typeof
---------
text
The result of your comparison of text
values depends on your locale, on COLLATION
rules to be precise. To see the default COLLATION
of your session:
SHOW lc_collate;
COLLATION
can be also be set per column or even per expression. To see the actual COLLATION
for any expression use the collation for (any)
construct:
SELECT collation for (my_column) FROM tbl LIMIT 1;
To test how strings are sorted in your locale:
SELECT *
FROM (
VALUES
('20150526')
, ('2015-05-25')
, ('20150525')
, ('2015-05-26')
, ('20150527')
, ('2015-05-27')
) t(col)
ORDER BY col; -- COLLATE "C"
Compare this to the sort order by pure byte-value by appending COLLATE "C"
to ORDER BY
. To see the result of your comparison with a different COLLATION
:
SELECT '20150526' > '2015-05-26' COLLATE "C" AS text2text_c;
So en_US.UTF-8
and en_US.utf8
have different sort orders?
No, these both are the same, just a different naming convention.
I'm mystified by why the Debian version appears to be case-insensitive and the OS X version is not.
Yes, you are correct. This is the default behavior on Mac. Collations don't work on any BSD-ish OS (incl. OSX) for UTF8
encoding.
Here is a reference to prove that:
Problems with sort order (UTF8 locales don't work
As a_horse_with_no_name said, Postgres uses the collation implementation from the OS. There is no way to get the same result on both operating systems.
In your case you may(I said maybe)do like this: ORDER BY lower(fieldname)
.
Best Answer
PostgreSQL does not support
=
orLIKE
onCOLLATE
. This is because internally index ordering uses=
and so even if the collation returns that they're equal PostgreSQL falls back to binary equal. This is documented,PostgreSQL also doesn't support Unicode collation in character classes.