
- 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 cut part of a string with a MySQL query?
For this, use substring_index() function from MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('STU-1011'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('STU-95968686'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | StudentId | +--------------+ | STU-1011 | | STU-95968686 | +--------------+ 2 rows in set (0.00 sec)
Following is the query to cut part of a string in MySQL before hyphen (-) −
mysql> select substring_index(substring(StudentId, instr(StudentId, "-")+1), " ", 1) from DemoTable;
This will produce the following output displaying part of the string before hyphen (-)
+------------------------------------------------------------------------+ | substring_index(substring(StudentId, instr(StudentId, "-")+1), " ", 1) | +------------------------------------------------------------------------+ | 1011 | | 95968686 | +------------------------------------------------------------------------+ 2 rows in set (0.02 sec)
- Related Articles
- PHP – How to cut out part of a string using iconv_substr()?
- MySQL query to replace part of string before dot
- How to select part of a Timestamp in a MySQL Query?
- MySQL query to change a string by displaying only the part of string after underscore?
- How to order by certain part of a string in MySQL?
- MySQL query to select a specific string with special characters
- Getting last 5 character of a string with MySQL query?
- How to replace a part of the string (domain name after @) using MySQL?
- Split the left part of a string by a separator string in MySQL?
- How to concatenate MySQL distinct query results into a string?
- MySQL query to retrieve records from the part of a comma-separated list?
- Replace part of a string with another string in C++
- Update a column value, replacing part of a string in MySQL?
- Get part of a string based on a character in MySQL?
- Query in MySQL for string fields with a specific length?

Advertisements