1

I am trying to sort a table by name. The name are in Thai character;

>Select name from product_categ
                    name                    
--------------------------------------------
 ระงับกลิ่นกาย
 สกินแคร์ (ดูแลผิวหน้า)
 รองเท้า
 ผู้หญิง
 เมคอัพ
 น้ำหอม
 วิตามิน อาหารเสริม
 เส้นบุก, บุกก้อน

When I try to sort the table by name, it does not work.(it does sort but is sorting in some different way / probably using the equivalent of Thai Character ?)

>Select name from product_categ order by name;
                    name                    
--------------------------------------------
 ชา กาแฟ เครื่องดื่มชงสำเร็จ
 ร่ม กันฝน พวงกุญแจ
 หมวก
 หมวก
 เล็บ
 แป้ง

So i tried to google and find a solution where i came across using collation directly in the query.However the query is not working for me.

select name from product_categ order by name COLLATE 'th_TH';

I receive and error mentioning error at 'th_th';

Any suggestion to fix this will be a much appreciated.

2
  • I am using ubuntu 12 Mar 1, 2017 at 13:44
  • 1
    first try to install th_TH.UTF-8 locale by using dpkg-reconfigure locales if you haven't. then you should restart postgresql.
    – Sahap Asci
    Mar 1, 2017 at 15:05

2 Answers 2

1

You need to specify that it's a UTF-8 collation:

SELECT name
FROM product_categ
ORDER BY name COLLATE "th_TH.UTF-8";
2
1

I created a new DB using the command below;

CREATE DATABASE testdb WITH
  TEMPLATE = template0
  ENCODING = 'UTF8'
  LC_COLLATE = 'th_TH.UTF-8'
  LC_CTYPE = 'th_TH.UTF-8';

After that I run this query in testdb;

SELECT
  word, upper(word)
FROM (
VALUES ('ชา กาแฟ เครื่องดื่มชงสำเร็จ'), ('ร่ม กันฝน พวงกุญแจ'), ('หมวก'), ('หมวก'), ('เล็บ'), ('แป้ง')) AS
  words(word)
ORDER BY 1; 

And I got following result;

          word          |         upper
------------------------+------------------------
 ชา กาแฟ เครื่องดื่มชงสำเร็จ | ชา กาแฟ เครื่องดื่มชงสำเร็จ
 ร่ม กันฝน พวงกุญแจ        | ร่ม กันฝน พวงกุญแจ
 หมวก                   | หมวก
 หมวก                   | หมวก
 เล็บ                    | เล็บ
 แป้ง                    | แป้ง
(6 rows)

I don't know your alphabet but if its ok, I can edit this post with details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.