
- 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 update a value with substring of current value by removing the separator and numbers after a separator in MySQL?
Here, let’s say you have a string with form “StringSeparatorNumber” form like John/56989. Now if you want to remove the number after separator /, then use the SUBSTRING_INDEX(). Let us first create a table −
mysql> create table DemoTable ( StudentName varchar(100) ); Query OK, 0 rows affected (1.05 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John/56989'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('David/74674'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Bob/45565'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | John/56989 | | Carol | | David/74674 | | Bob/45565 | +-------------+ 4 rows in set (0.00 sec)
Following is the query to update a value with substring of current value −
mysql> update DemoTable set StudentName=substring_index(StudentName,'/',1); Query OK, 3 rows affected (0.13 sec) Rows matched :4 Changed :3 Warnings :0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | John | | Carol | | David | | Bob | +-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Get the substring after the first occurrence of a separator in Java
- How to add separator to numbers using MySQL views?
- How to place a thousand separator in MySQL records?
- Get the substring before the last occurrence of a separator in Java
- Split the left part of a string by a separator string in MySQL?
- Display numbers with thousands separator in Java
- Possible to split a string with separator after every word in JavaScript
- How to add separator in a ToolBar with Java?
- Splitting a slice of bytes after the specified separator in Golang
- How to update a MySQL column by subtracting a value with some conditions?
- How to use GROUP BY to concatenate strings in MySQL and how to set a separator for the concatenation?
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL group_concat to add a separator for empty fields?
- Splitting the string after the specified separator in Golang
- Splitting the slice after the specified separator in Golang

Advertisements