วันเสาร์ที่ 31 สิงหาคม พ.ศ. 2556

นับ string(utf-8) ใน MySQL

- http://www.electrictoolbox.com/find-length-longest-string-mysql/

- SELECT subdistrictname, LENGTH(subdistrictname) FROM subdistrict;
ได้ผลคือ
แขวงโคกแฝด (length = 30)

มันไม่ตรงกับที่เราต้องการนี่นา แขวงโคกแฝด นับด้วยตาแล้วได้แค่ 10 อักษรเองนิ
ก็เลยไปค้น google ได้เจอเกี่ยวกับการนับข้อความ UTF-8
http://www.thaicreate.com/php/forum/022048.html

ตามคำตอบของคุณ เต๋า ในกระทู้ thaicreate เปลี่ยนจากโค้ดด้านบนเป็น...
- SELECT (LENGTH(CONVERT(subdistrictname USING 'ucs2'))/2) FROM subdistrict;

ผลลัพธ์ที่ได้



ข้อมูลเพิ่มเติมเกี่ยวกับ ucs2
Unicode UCS-2 code chart
http://dev.mysql.com/doc/refman/4.1/en/charset-unicode-ucs2.html