
- 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
How to get first N characters from a MySQL column?
Use SUBSTRING() to get first N characters from a MySQL column. Let us first create a table −
mysql>create table DemoTable ( Information text ); Query OK, 0 rows affected (2.63 sec)
Insert records in the table using insert command −
mysql>insert into DemoTable values('MySQL is a structured query language'); Query OK, 1 row affected (0.13 sec)
Following is the query to display all records from the table using select statement −
mysql>select *from DemoTable;
This will produce the following output −
+--------------------------------------+ | Information | +--------------------------------------+ | MySQL is a structured query language | +--------------------------------------+ 1 row in set (0.00 sec)
Here is the query to get first N characters from the column. In this case, we are selecting the first 10 characters −
mysql>select substring(Information,1,10) from DemoTable;
This will produce the following output −
+-----------------------------+ | substring(Information,1,10) | +-----------------------------+ | MySQL is a | +-----------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to select all the characters after the first 20 characters from a column in MySQL?
- MySQL query to get first two highest column values from a table?
- How to extract the first n characters from a string using Java?
- Return only the first 15 characters from a column with string values in MySQL
- JavaScript - Remove first n characters from string
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- MySQL query to remove special characters from column values?
- MySQL query to replace special characters from column value
- How can we get some last number of characters from the data stored in a MySQL table’s column?
- How can we get some starting number of characters from the data stored in a MySQL table’s column?
- Return the first n letters of a column in MySQL
- How to get a specific column record from SELECT query in MySQL?
- MySQL query to keep only first 2 characters in column value and delete rest of the characters?
- Get first date from timestamp in MySQL group by another column with duplicate value
- How to extract first two characters from a string in R?

Advertisements