ฟังก์ชัน SUBSTRING() (หรือ SUBSTR()) ใน MySQL รองรับรูปแบบ FROM pos สำหรับดึงข้อความตั้งแต่ตำแหน่งที่กำหนดจนจบสตริง
ตัวอย่างการสร้างตาราง
mysql> create table DemoTable (
Title text );
Query OK, 0 rows affected (0.86 sec)
เพิ่มข้อความยาวต่างๆ
mysql> insert into DemoTable values('C is a good programming language to start');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('Java is good with data structure and algorithm');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Coding is very important');
Query OK, 1 row affected (0.20 sec)
แสดงข้อมูลทั้งหมด
mysql> select * from DemoTable;
+------------------------------------------------+ | Title | +------------------------------------------------+ | C is a good programming language to start | | Java is good with data structure and algorithm | | Coding is very important | +------------------------------------------------+ 3 rows in set (0.00 sec)
คำสั่งดึงข้อความตั้งแต่ตำแหน่งที่ 20 จนจบ
mysql> select substring(Title from 20) from DemoTable;
ผลลัพธ์
+-----------------------------+ | substring(Title from 20) | +-----------------------------+ | ming language to start | | ata structure and algorithm | | rtant | +-----------------------------+ 3 rows in set (0.00 sec)
คำอธิบายรูปแบบ:
SUBSTRING(str FROM pos)- ดึงตั้งแต่ตำแหน่ง pos จนจบ (Standard SQL)SUBSTRING(str, pos)- รูปแบบ MySQL เหมือนข้างต้นSUBSTRING(str, pos, len)- ดึงยาว len ตัวอักษรSUBSTRING(str FROM pos FOR len)- รูปแบบ Standard SQL ระบุความยาว
หมายเหตุ: ตำแหน่งเริ่มที่ 1 ดังนั้น FROM 20 = ข้าม 19 ตัวอักษรแรก