
- 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 extract first word from a field?
To extract first word from a field, use in-built SUBSTRING_INDEX() function. The syntax is as follows −
SELECT SUBSTRING_INDEX(yourColumnName,’ ‘,1) as anyVariableName from yourTableName;
In the above query, if you use -1 in place of 1 then you will get the last word. To understand the above concept, let us create a table. The following is the query to create a table.
mysql> create table FirstWordDemo −> ( −> AllWords longtext −> ); Query OK, 0 rows affected (0.83 sec)
Now insert some words in the table using insert command. The query is as follows −
mysql> insert into FirstWordDemo values('This is the first MySQL Query'); Query OK, 1 row affected (0.11 sec) mysql> insert into FirstWordDemo values('MySQL is a Relational Database'); Query OK, 1 row affected (0.17 sec) mysql> insert into FirstWordDemo values('FirstWord is not correct'); Query OK, 1 row affected (0.21 sec)
Now display all records from the table using select statement. The query is as follows −
mysql> select *from FirstWordDemo;
The following is the output −
+--------------------------------+ | AllWords | +--------------------------------+ | This is the first MySQL Query | | MySQL is a Relational Database | | FirstWord is not correct | +--------------------------------+ 3 rows in set (0.00 sec)
Here is the query to get first word from a field. We discussed the same syntax in the beginning.
The following is the query −
mysql> select SUBSTRING_INDEX(AllWords, ' ', 1) as MyFirstWordResult from FirstWordDemo;
The following is the output −
+-------------------+ | MyFirstWordResult | +-------------------+ | This | | MySQL | | FirstWord | +-------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to extract last word from a field?
- Select first word in a MySQL query?
- How to remove only the first word from columns values with a MySQL query?
- MySQL query to search exact word from string?
- How to extract only the numbers from a text field in MySQL?
- MySQL query to count comma’s from field value?
- MySQL query to check if a string contains a word?
- MySQL query to get first two highest column values from a table?
- MySQL query to extract time in a format without seconds
- MySQL query to display the first alphabet from strings in a separate column
- MySQL query to remove first digit?
- How to extract first value from a list in R?
- Remove 20% from stored price in MySQL field, using SQL query?
- How to add a day to datetime field in MySQL query?
- How to extract first two characters from a string in R?

Advertisements