
- 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 substring value in a MySQL table column?
To substring a MySQL table column, use the in-built SUBSTR() function from MySQL. The syntax is as follows −
select substr(yourColumnName,AnyValue) as anyVariableName from yourTableName;
To understand the function substr(), let us create a table. The query to create a table is as follows −
mysql> create table SubStringDemo −> ( −> UserId varchar(200) −> ); Query OK, 0 rows affected (0.55 sec)
Now insert some records in the table. The query to insert records is as follows −
mysql> insert into SubStringDemo values('Bob10015'); Query OK, 1 row affected (0.29 sec) mysql> insert into SubStringDemo values('Smith0015'); Query OK, 1 row affected (0.22 sec) mysql> insert into SubStringDemo values('Carol20010'); Query OK, 1 row affected (0.14 sec)
Now you can display all records from the table with the help of select statement. The query is as follows −
mysql> select *from SubStringDemo;
The following is the output −
+------------+ | UserId | +------------+ | Bob10015 | | Smith0015 | | Carol20010 | +------------+ 3 rows in set (0.00 sec)
The following is the query to substring MySQL table column −
mysql> select substr(UserId,5) as ExtractSubstring from SubStringDemo;
Here is the output that displays the substrings −
+------------------+ | ExtractSubstring | +------------------+ | 0015 | | h0015 | | l20010 | +------------------+ 3 rows in set (0.00 sec)
- Related Articles
- How can we extract a substring from the value of a column in MySQL table?
- Find rows where column value ends with a specific substring in MySQL?
- Setting similar value for a column in a MySQL table?
- Sort a MySQL table column value by part of its value?
- How to add a column in a table in MySQL?
- Get the substring of a column in MySQL
- Check if a value exists in a column in a MySQL table?
- How to delete a column from a table in MySQL?
- How to add a column to a MySQL table in Python?
- How to rename a column in an existing MySQL table?
- How to append 000 in a MySQL column value?
- How to check if a column exist in a MySQL table?
- How to get substring results from a table with file location recordsi in MySQL?
- How to prepend a string to a column value in MySQL?
- How to add subtotal to a table column displaying NULL in MySQL?

Advertisements