- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return only the first 15 characters from a column with string values in MySQL
To return only the first 15 characters from string values, use the MySQL SUBSTR() function.
Let us first create a table −
mysql> create table DemoTable ( Title varchar(100) ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Introduction to Java'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C in Depth with data structure and algorithm'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------------------------------------------+ | Title | +----------------------------------------------+ | Introduction to MySQL | | Introduction to Java | | C in Depth with data structure and algorithm | +----------------------------------------------+ 3 rows in set (0.00 sec)
Let us now implement the query to fetch only the first 15 characters −
mysql> select substr(Title,1,15) from DemoTable;
This will produce the following output −
+--------------------+ | substr(Title,1,15) | +--------------------+ | Introduction to | | Introduction to | | C in Depth with | +--------------------+ 3 rows in set (0.00 sec)
Advertisements