ข้ามไปที่เนื้อหาหลัก

ปัญหา SQL Server 2008 ใช้ rtrim แล้วไม่ยอมตัด space ต่อท้าย field

เกิดจาก space ใน sql server มี 2 แบบ คือ soft space กับ  hard space (ที่มา) ตัว soft space มักจะเกิดตอน import ข้อมูล โดยมีรหัส ascii คือ 160 แต่ rtrim ทำงานกับ ascii 32 จึงใช้ด้วยกันไม่ได้ แก้โดยใช้ replace เป็น soft space เป็น hard space ก่อน ดังนี้

update the_table
set field1 = rtrim(replace(field1,char(160),' '))
where (ascii(right(field1,1))=160)

ตรวจสอบโดย
select field1, ascii(right(field1,1)) from thetable where ascii(right(field1,1)) = 160

ถ้าทำสำเร็จจะได้ 0 rows effected

ความคิดเห็น