- 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
MySQL query to group rows by the numeric characters in a string field?
Fir this, you can concatenate 0 with string field with the help of + operator. The scenario here is like we need to fetch numeric “9844” from a string field “9844Bob”.
Let us first create a table −
mysql> create table DemoTable ( StudentId varchar(100) ); Query OK, 0 rows affected (0.92 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('9844Bob'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('6375DavidMiller'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('007'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('97474BobBrown'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('9844Bob56Taylor'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------------+ | StudentId | +-----------------+ | 9844Bob | | 6375DavidMiller | | 007 | | 97474BobBrown | | 9844Bob56Taylor | +-----------------+ 5 rows in set (0.00 sec)
Following is the query to group rows by the numeric characters in a string field −
mysql> select StudentId,0+StudentId as GroupValue from DemoTable;
This will produce the following output −
+-----------------+------------+ | StudentId | GroupValue | +-----------------+------------+ | 9844Bob | 9844 | | 6375DavidMiller | 6375 | | 007 | 7 | | 97474BobBrown | 97474 | | 9844Bob56Taylor | 9844 | +-----------------+------------+ 5 rows in set, 4 warnings (0.00 sec)
- Related Articles
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- MySQL query to return only the rows which are numeric?
- MySQL query to update string field by concatenating to it?
- MySQL query to find all rows where string contains less than four characters?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- MySQL query to get the max value with numeric values in varchar field?
- How to count all characters in all rows of a field in MySQL?
- Listing all rows by group with MySQL GROUP BY?
- MySQL query to display records ordered by numeric difference?
- Get rows with GROUP BY in MySQL?
- MySQL query to GROUP BY multiple columns
- MySQL: update field with Group By?
- MySQL query to select rows where column value is only 0, group by another column?
- MySQL query to select a specific string with special characters
- C++ Program to remove Characters from a Numeric String Such That String Becomes Divisible by 8

Advertisements