- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select first word in a MySQL query?
To select first word in MySQL query, you can use SUBSTRING_INDEX().
Following is the syntax −
select substring_index(yourColumnName,' ',1) as anyAliasName from yourTableName;
Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFullName varchar(40) ); Query OK, 0 rows affected (0.61 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(StudentFullName) values('John Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFullName) values('Carol Taylor'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentFullName) values('Bob Williams'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentFullName) values('David Miller'); Query OK, 1 row affected (0.16 sec)
Display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-----------------+ | StudentId | StudentFullName | +-----------+-----------------+ | 1 | John Smith | | 2 | Carol Taylor | | 3 | Bob Williams | | 4 | David Miller | +-----------+-----------------+ 4 rows in set (0.00 sec)
Following is the query to select first word in MySQL query −
mysql> select substring_index(StudentFullName,' ',1) as FirstWord from DemoTable;
This will produce the following output displaying the first word −
+-----------+ | FirstWord | +-----------+ | John | | Carol | | Bob | | David | +-----------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to extract first word from a field?
- How to write a MySQL query to select first 10 records?
- MySQL query to select rows except first row in descending order?
- How to remove only the first word from columns values with a MySQL query?
- Insert with a Select query in MySQL
- A single MySQL query to select value from first table and insert in the second?
- MySQL query to extract last word from a field?
- Output only the first word from select list value - jQuery?
- MySQL query to check if a string contains a word?
- Select first element of a commaseparated list in MySQL?
- Select query with regular expression in MySQL
- Select the table name as a column in a UNION select query with MySQL?
- MySQL UNION SELECT and IN clause in a single query
- MySQL select query with multiple WHERE?
- Can we use SELECT NULL statement in a MySQL query?

Advertisements