
- 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
MySQL query to display a substring before a special character in a string
Use the LOCATE() and SUBSTRING() method for this in MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Introduction To Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Introduction - To MySQL'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------------------------+ | Title | +-------------------------+ | Introduction To Java | | Introduction - To MySQL | +-------------------------+ 2 rows in set (0.00 sec)
Following is the query to display a substring before a special character in a substring −
mysql> select distinct -> if(LOCATE(' - ',Title)>0, SUBSTRING(Title, 1, LOCATE(' - ', Title)), Title) from DemoTable;
Output
This will produce the following output −
+-----------------------------------------------------------------------------+ | if(LOCATE(' - ',Title)>0, SUBSTRING(Title, 1, LOCATE(' - ', Title)), Title) | +-----------------------------------------------------------------------------+ | Introduction To Java | | Introduction | +-----------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to get all characters before a specific character hyphen
- MySQL query to select a specific string with special characters
- C# Program to replace a special character from a String
- MySQL query to get a substring from a string except the last three characters?
- MySQL ORDER BY CASE to display special character in the beginning
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character
- PHP program to check if a string has a special character
- MySQL query to check if a string contains a value (substring) within the same row?
- MySQL query to return a substring after delimiter?
- Getting last 5 character of a string with MySQL query?
- How to remove partial string after a special character in R?
- Program to check if a string contains any special character in C
- Program to check if a string contains any special character in Python
- C# program to check if a string contains any special character
- Java program to check if a string contains any special character

Advertisements