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.

Ian_H
  • 1,646
  • 10
  • 17
Sijan Shrestha
  • 313
  • 5
  • 15

2 Answers2

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";
Randolph West
  • 3,683
  • 11
  • 26
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.

Sahap Asci
  • 2,889
  • 1
  • 14
  • 26