Sử dụng hàm LEFT() để cắt lấy số ký tự mong muốn từ đầu chuỗi.
Cú pháp
SELECT LEFT(tenCot, 200) AS tenAlias FROM tenBang;
Ví dụ
Tạo bảng kiểu LONGTEXT và chèn dữ liệu dài:
mysql> create table DemoTable (Paragraph longtext); Query OK, 0 rows affected (0.71 sec)
mysql> insert into DemoTable values('Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to ASP.net,Introduction to JSF');
Query OK, 1 row affected (0.13 sec)Lấy 200 ký tự đầu:
mysql> select left(Paragraph, 200) AS `200Characters` from DemoTable;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 200Characters | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Ser | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Lưu ý: LEFT() an toàn với chuỗi ngắn hơn độ dài chỉ định (sẽ trả về toàn bộ chuỗi). Hàm tương tự: RIGHT() lấy từ cuối, SUBSTRING() lấy đoạn giữa.