

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I select only those rows where first digit is a number from 0 to 9 in MySQL?
To select only those rows where first digit is a number from 0 to 9, use RLIKE.
Following is the syntax −
select *from yourTableName where yourColumnName RLIKE '^[0-9]+'
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, QuestionNumber varchar(200) ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(QuestionNumber) values('1Question'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(QuestionNumber) values('Question2'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(QuestionNumber) values('311Question'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(QuestionNumber) values('45Question'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(QuestionNumber) values('Question10'); Query OK, 1 row affected (0.13 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+----------------+ | Id | QuestionNumber | +----+----------------+ | 1 | 1Question | | 2 | Question2 | | 3 | 311Question | | 4 | 45Question | | 5 | Question10 | +----+----------------+ 5 rows in set (0.00 sec)
Here is the query to select only those rows where first digit is a number from 0 to 9 −
mysql> select *from DemoTable where QuestionNumber RLIKE '^[0-9]+';
This will produce the following output −
+----+----------------+ | Id | QuestionNumber | +----+----------------+ | 1 | 1Question | | 3 | 311Question | | 4 | 45Question | +----+----------------+ 3 rows in set (0.03 sec)
- Related Questions & Answers
- MySQL query to select rows where column value is only 0, group by another column?
- Fetch rows where first character is not alphanumeric in MySQL?
- How to select only 3 ordered rows on a MySQL table?
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Can I use two where clauses like “SELECT * FROM table WHERE condition1 and condition2” in MySQL?
- How to select last 10 rows from MySQL?
- How do I select 5 random rows from the 20 most recent rows in MySQL?
- How can we use MySQL SELECT statement to count number of rows in a table?
- How to select first 10 elements from a MySQL database?
- How can I select rows which fall on a specific day of week in MySQL?
- Generate random number from 0 – 9 in PHP?
- How to select only MySQL date from datetime column?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- How do I create a random four-digit number in MySQL?
- How can I generate random integers between 0 and 9 using Python?
Advertisements