- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 select all fields beginning with a given number with next character a letter?
You can use REGEXP for this. Let us first create a table −
mysql> create table DemoTable ( Value text ); Query OK, 0 rows affected (1.28 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values('645st'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('765stp'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('665tcp'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('606cpp'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+--------+ | Value | +--------+ | 645st | | 765stp | | 665tcp | | 606cpp | +--------+ 4 rows in set (0.00 sec)
Following is the query to select all fields beginning with a given number with the next character a letter −
mysql> select *from DemoTable WHERE Value REGEXP '^6[[:alnum:]]';
This will produce the following output −
+--------+ | Value | +--------+ | 645st | | 665tcp | | 606cpp | +--------+ 3 rows in set (0.01 sec)
Advertisements