Limit length of longtext field in MySQL SELECT results?

MySQLMySQLi Database

You can use SUBSTRING() from MySQL to limit length of strings. The syntax is as follows

SELECT SUBSTRING(yourColumnName,1,yourIntegerValueToGetTheCharacters) as
anyVariableName from yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table limitLengthOfLongTextDemo
   -> (
   -> sentence LONGTEXT
   -> );
Query OK, 0 rows affected (0.74 sec)

Insert some records in the table using insert command. The query is as follows

mysql> insert into limitLengthOfLongTextDemo values('This is the introduction to MySQL');
Query OK, 1 row affected (0.17 sec)

mysql> insert into limitLengthOfLongTextDemo values('PL/SQL is the extension of Structured
Query Language');
Query OK, 1 row affected (0.19 sec)

mysql> insert into limitLengthOfLongTextDemo values('Java is an Object Oriented
Programming Language');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement. The query is as follows

mysql> select *from limitLengthOfLongTextDemo;

The following is the output

+------------------------------------------------------+
| sentence                                             |
+------------------------------------------------------+
| This is the introduction to MySQL                    |
| PL/SQL is the extension of Structured Query Language |
| Java is an Object Oriented Programming Language      |
+------------------------------------------------------+
3 rows in set (0.00 sec)

Here is the query to get the characters of the given value

mysql> select substring(sentence,1,26) as 26Characters from limitLengthOfLongTextDemo;

The following is the output

+----------------------------+
| 26Characters               |
+----------------------------+
| This is the introduction t |
| PL/SQL is the extension of |
| Java is an Object Oriented |
+----------------------------+
3 rows in set (0.00 sec)
raja
Published on 16-Jan-2019 12:54:15
Advertisements