
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- MySQL query to retrieve only the column values with special characters?
- MySQL query to find the average of only first three values from a column with five values
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- MySQL query to return the count of only NO values from corresponding column value
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- How to get first N characters from a MySQL column?
- How to select all the characters after the first 20 characters from a column in MySQL?
- How to order return duplicate column values only once in MySQL?
- MySQL query to keep only first 2 characters in column value and delete rest of the characters?
- How to remove only the first word from columns values with a MySQL query?
- MySQL query to remove special characters from column values?
- MySQL regular expression to update a table with column values including string, numbers and special characters
- Return the first n letters of a column in MySQL
- Find and display duplicate values only once from a column in MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL

Advertisements