
- 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
Get the index of last substring in a given string in MySQL?
To get the index of last substring in a given string, use the char_length() function. First, we need to calculate string length and subtract the last sub string length from the entire length. The difference in length is index of substring.
Syntax
The syntax is as follows −
select CHAR_LENGTH(yourColumnName) - LOCATE('yourDelimiter ', REVERSE(yourColumnName))+1 as anyVariableName from yourTableName;
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table SubStringIndexDemo -> ( -> Words varchar(200) -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using the insert command. The query is as follows −
mysql> insert into SubStringIndexDemo values('This is MySQL Query'); Query OK, 1 row affected (0.13 sec) mysql> insert into SubStringIndexDemo values('MySQL is a Relational Database'); Query OK, 1 row affected (0.34 sec) mysql> insert into SubStringIndexDemo values('Java is a programming language'); Query OK, 1 row affected (0.11 sec) mysql> insert into SubStringIndexDemo values('Spring is a Framework'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select command. The query is as follows −
mysql> select *from SubStringIndexDemo;
Output
+--------------------------------+ | Words | +--------------------------------+ | This is MySQL Query | | MySQL is a Relational Database | | Java is a programming language | | Spring is a Framework | +--------------------------------+ 4 rows in set (0.00 sec)
Here is the query that can be used to get the index of the last substring::
mysql> select CHAR_LENGTH(Words) - LOCATE(' ', REVERSE(Words))+1 as PositionOfLastSubstring -> from SubStringIndexDemo;
Output
+-------------------------+ | PositionOfLastSubstring | +-------------------------+ | 14 | | 22 | | 22 | | 12 | +-------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL String Last Index Of in a URL?
- Get the last occurrence of a substring within a string in Arduino
- Golang Program to get the index of the substring in a string
- Python Program to get the index of the substring in a string
- How to find index of last occurrence of a substring in a string in Python?
- MySQL query to get a substring from a string except the last three characters?
- Get the substring of a column in MySQL
- Get the substring before the last occurrence of a separator in Java
- How to get the last index of an occurrence of the specified value in a string in JavaScript?
- Fetch the substring after last dot in MySQL
- Find last index of a character in a string in C++
- How to get last 12 digits from a string in MySQL?
- How to get substring of a string in jQuery?
- Get the last index of a particular element in an ArrayList in Java
- How to apply Substring() for fields in MySQL to get part of string?

Advertisements