
- 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 remove -XXX from Zip Code field using MySQL REGEXP?
The easiest way to achieve this is by using the MySQL SUBSTRING_INDEX() function. Let us first create a table −
mysql> create table DemoTable ( ZipCode varchar(50) ); Query OK, 0 rows affected (2.02 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('52533-909'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('12345-674'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('89893-890'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('AAAAA-783'); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | ZipCode | +-----------+ | 52533-909 | | 12345-674 | | 89893-890 | | AAAAA-783 | +-----------+ 4 rows in set (0.00 sec)
Following is the query to remove -XXX from zipcode using substring_index() −
mysql> update DemoTable set ZipCode=substring_index(ZipCode, '-', 1); Query OK, 4 rows affected (0.44 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 −
+---------+ | ZipCode | +---------+ | 52533 | | 12345 | | 89893 | | AAAAA | +---------+ 4 rows in set (0.00 sec)
- Related Articles
- How to front pad zip code with “0” in MySQL?
- How to remove special characters from a database field in MySQL?
- Remove 20% from stored price in MySQL field, using SQL query?
- Remove seconds from time field in MySQL?
- Zip Code validation using Java Regular Expressions
- How to remove leading and trailing whitespace from a MySQL field value?
- How to Convert Zip Code to State in Excel?
- How to remove HTML Tags with RegExp in JavaScript?
- How to remove leading and trailing whitespace in a MySQL field?
- How to remove a field completely from a MongoDB document?
- How to remove duplicate values from a MySQL table using LEFT JOIN?
- How to remove hyphens using MySQL UPDATE?
- Get only digits using regexp in MySQL?
- How to derive value of a field from another field in MySQL?
- How to use RLIKE/REGEXP patterns in MySQL?

Advertisements