
- 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 existing column data in MySQL and remove the last string from a varchar column with strings and numbers
Let us first create a table −
mysql> create table DemoTable ( Download varchar(100) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('120 Gigabytes'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('190 Gigabytes'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('250 Gigabytes'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('1000 Gigabytes'); 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 −
+----------------+ | Download | +----------------+ | 120 Gigabytes | | 190 Gigabytes | | 250 Gigabytes | | 1000 Gigabytes | +----------------+ 4 rows in set (0.00 sec)
Following is the query to update existing column data and remove the last string −
mysql> update DemoTable set Download=LEFT(Download, INSTR(Download, ' ') - 1); Query OK, 4 rows affected (0.16 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 −
+----------+ | Download | +----------+ | 120 | | 190 | | 250 | | 1000 | +----------+ 4 rows in set (0.00 sec)
- Related Articles
- How do I remove ON UPDATE CURRENT_TIMESTAMP from an existing column in MySQL?
- Update a column in MySQL and remove the trailing underscore values
- How to increase varchar size of an existing column in a database without breaking existing data in MySQL?
- MySQL regular expression to update a table with column values including string, numbers and special characters
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- Python Pandas – Remove numbers from string in a DataFrame column
- MySQL query to replace backslash from a varchar column with preceding backslash string values
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- Order VARCHAR records with string and numbers in MySQL
- Find maximum value from a VARCHAR column in MySQL
- Remove trailing numbers surrounded by parenthesis from a MySQL column
- How to update records in a column with random numbers in MySQL?
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- How to set a string with hyphen and numbers in MySQL varchar?
- How to cast and update a numeric value from string column only where applicable in MySQL?

Advertisements