
- 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
Update all varchar column rows to display values before slash in MySQL?
For this, use UPDATE command along with SUBSTRING_INDEX(). Let us first create a table −
mysql> create table demo69 −> ( −> name varchar(40) −> ); Query OK, 0 rows affected (5.04 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo69 values('John/Smith'); Query OK, 1 row affected (0.83 sec) mysql> insert into demo69 values('David/Miller'); Query OK, 1 row affected (0.23 sec) mysql> insert into demo69 values('Chris/Brown'); Query OK, 1 row affected (0.40 sec) mysql> insert into demo69 values('Carol/Taylor'); Query OK, 1 row affected (0.36 sec)
Display records from the table using select statement −
mysql> select *from demo69;
This will produce the following output −
+--------------+ | name | +--------------+ | John/Smith | | David/Miller | | Chris/Brown | | Carol/Taylor | +--------------+ 4 rows in set (0.03 sec)
Following is the query to update all varchar column rows −
mysql> update demo69 −> set name=substring_index(name,'/',1); Query OK, 4 rows affected (0.13 sec) Rows matched: 4 Changed: 4 Warnings: 0
Display records from the table using select statement −
mysql> select *from demo69;
This will produce the following output −
+-------+ | name | +-------+ | John | | David | | Chris | | Carol | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- How to sum varchar column and display the count in MySQL?
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- Update multiple rows in a single column in MySQL?
- How to update a column of varchar type in MySQL to increase its length?
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
- MySQL update column to NULL for blank values
- How to select only non - numeric values from varchar column in MySQL?
- How to update multiple rows and left pad values in MySQL?
- How to display column values as CSV in MySQL?
- MySQL query to update all the values in a column with numeric incremental values like John1, John2, John3, etc.
- Update all rows by prefixing a line to every text in MySQL?
- Display all the column values in a single row separated by comma in MySQL?
- Fix a specific column value and display random values for rest of the rows in MySQL
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- How to update a MySQL table by swapping two column values?

Advertisements