
- 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
Is it possible to delete everything after a 'space' in a MySQL field?
In order to delete everything after a space, you need to use SUBSTRING_INDEX().
The syntax is as follows
select substring_index(yourColumnName,' ',1) as anyAliasName from yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table deleteAfterSpaceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into deleteAfterSpaceDemo(StudentName) values('John Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into deleteAfterSpaceDemo(StudentName) values('Adam Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into deleteAfterSpaceDemo(StudentName) values('Carol Taylor'); Query OK, 1 row affected (0.18 sec) mysql> insert into deleteAfterSpaceDemo(StudentName) values('Chris Brown'); Query OK, 1 row affected (0.14 sec) mysql> insert into deleteAfterSpaceDemo(StudentName) values('David Miller'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from deleteAfterSpaceDemo;
The following is the output
+----+--------------+ | Id | StudentName | +----+--------------+ | 1 | John Smith | | 2 | Adam Smith | | 3 | Carol Taylor | | 4 | Chris Brown | | 5 | David Miller | +----+--------------+ 5 rows in set (0.00 sec)
Here is the query to delete everything after a space
mysql> select substring_index(StudentName,' ',1) as deleteAllAfterSpace from deleteAfterSpaceDemo;
The following is the output
+---------------------+ | deleteAllAfterSpace | +---------------------+ | John | | Adam | | Carol | | Chris | | David | +---------------------+ 5 rows in set (0.04 sec)
- Related Articles
- Is it possible to rename _id field after MongoDB group aggregation?
- How to delete everything in a MongoDB database?
- Is it possible to calculate a correlation in a MySQL query?
- Is it possible to use MongoDB field value as a pattern in $regex?
- MySQL query to select everything to left of last space in a column with name records
- Is it possible to have a function-based index in MySQL?
- How to search for a record (field) and then delete it in MongoDB?
- Is it possible to use MongoDB field value as pattern in $regex?
- Fire trigger after the DELETE operation is executed in MySQL
- Is it possible to resume java execution after exception occurs?
- Check if it is possible to sort the array after rotating it in Python
- How is it possible for a MySQL trigger to execute multiple statements?
- Insert the results of a MySQL select? Is it possible?
- How to update a field with a particular value if it is null in MySQL?
- How can it be possible to invert a string in MySQL?

Advertisements