
- 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 change a string by displaying only the part of string after underscore?
Let us first create a table −
mysql> create table DemoTable ( UserName varchar(100) ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Smith_John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Smith_Adam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Smith_David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Smith_Mike'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | UserName | +-------------+ | Smith_John | | Smith_Adam | | Smith_David | | Smith_Mike | +-------------+ 4 rows in set (0.00 sec)
Following is the query to display only the part of the string after underscore −
mysql> update DemoTable set UserName=substring(UserName,7) where UserName LIKE 'Smith_%'; Query OK, 4 rows affected (0.12 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+----------+ | UserName | +----------+ | John | | Adam | | David | | Mike | +----------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to replace part of string before dot
- How to cut part of a string with a MySQL query?
- Split the left part of a string by a separator string in MySQL?
- How to replace a part of the string (domain name after @) using MySQL?
- How to match underscore in a MySQL String?
- How to order by certain part of a string in MySQL?
- Parse a string to get a number from a large string separated by underscore
- Fetch middle part of a string surrounded by slash in MySQL
- MySQL Query to remove all characters after last comma in string?
- MySQL query to replace a string after the last / in a column with directory links?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- MySQL query to sort by certain last string character?
- MySQL string manipulation to count only sub-part of duplicate values in IP ADDRESS records?
- How to get a part of string after a specified character in JavaScript?
- MySQL query to update string field by concatenating to it?

Advertisements