
- 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 remove numbers after hyphen in a VARCHAR string with numbers
For this, use SUBSTRING_INDEX(). Let us first create a table −
mysql> create table DemoTable2040 -> ( -> StudentCode varchar(20) -> ); Query OK, 0 rows affected (0.85 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2040 values('John-232'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2040 values('Carol-901'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2040 values('David-987'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2040;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | John-232 | | Carol-901 | | David-987 | +-------------+ 3 rows in set (0.00 sec)
Here is the query to remove numbers after hyphen −
mysql> update DemoTable2040 -> set StudentCode=substring_index(StudentCode,'-',1); Query OK, 3 rows affected (0.21 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable2040;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | John | | Carol | | David | +-------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to set a string with hyphen and numbers in MySQL varchar?
- Order VARCHAR records with string and numbers in MySQL
- MySQL query to remove a value with only numbers in a column
- Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?
- Extract numbers after the last hyphen in PHP?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- How to display highest value from a string with numbers set as varchar in MySQL?
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- MySQL Query to remove all characters after last comma in string?
- REGEX in MySQL to display only numbers separated by hyphen.
- Can we compare numbers in a MySQL varchar field?
- MySQL query to replace backslash from a varchar column with preceding backslash string values
- Concatenate string with numbers in MySQL?
- MySQL number-string formatting to pad zeros on the left of a string with numbers after a slash
- Set numbers as a varchar field and compare in MySQL

Advertisements