ฟังก์ชัน TRIM() ของ MySQL ใช้สำหรับลบ Suffix หรือ Prefix เฉพาะ หรือทั้งสองจากสตริง การทำงานของฟังก์ชัน TRIM() สามารถเข้าใจได้จากไวยากรณ์:
ไวยากรณ์
TRIM([{BOTH | LEADING | TRAILING} [str_to_remove] FROM] string)
ที่นี่:
- อาร์กิวเมนต์ BOTH หมายถึง Prefix จากทั้งซ้ายและขวาจะถูกลบออกจากสตริง
- อาร์กิวเมนต์ LEADING หมายถึงเฉพาะ Leading Prefix จะถูกลบ
- อาร์กิวเมนต์ TRAILING หมายถึงเฉพาะ Trailing Prefix จะถูกลบ
- Str_to_remove คืออาร์กิวเมนต์ที่หมายถึงสตริงที่เราต้องการลบจากสตริง
- String คืออาร์กิวเมนต์ที่หมายถึงสตริงที่ Prefix จะถูกลบออก
ตัวอย่าง
mysql> Select TRIM(BOTH '0' FROM '0100'); +----------------------------+ | TRIM(BOTH '0' FROM '0100') | +----------------------------+ | 1 | +----------------------------+ 1 row in set (0.00 sec) mysql> Select TRIM(BOTH '**' from '**tutorialspoint.com**'); +------------------------------------------------+ | TRIM(BOTH '**' from '**tutorialspoint.com**') | +------------------------------------------------+ | tutorialspoint.com | +------------------------------------------------+ 1 row in set (0.00 sec) mysql> Select TRIM(Trailing '**' from '**tutorialspoint.com**'); +----------------------------------------------------+ | TRIM(Trailing '**' from '**tutorialspoint.com**') | +----------------------------------------------------+ | **tutorialspoint.com | +----------------------------------------------------+ 1 row in set (0.00 sec) mysql> Select TRIM(Leading '**' from '**tutorialspoint.com**'); +---------------------------------------------------+ | TRIM(Leading '**' from '**tutorialspoint.com**') | +---------------------------------------------------+ | tutorialspoint.com** | +---------------------------------------------------+ 1 row in set (0.00 sec)