Searching for an integer value in a varchar field in MySQL?


To search for an integer value in a varchar filed, you can use CASE statement.

Let us first create a table. Consider, we have a list of email-ids −

mysql> create table DemoTable
-> (
-> Title varchar(100)
-> );
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('9John@example.com');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('3Carol@gmail.com');
Query OK, 1 row affected (0.45 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-------------------+
| Title             |
+-------------------+
| 9John@example.com |
| 3Carol@gmail.com  |
+-------------------+
2 rows in set (0.00 sec)

Here is the query to searching for an integer value in a varchar field in MySQL −

mysql> select (case when Title = 9 then 'Search Successful' else 'Search Unsuccessful' end)
AS Result from DemoTable;

Output

This will produce the following output −

+---------------------+
| Result              |
+---------------------+
| Search Successful   |
| Search Unsuccessful |
+---------------------+
2 rows in set, 2 warnings (0.00 sec)

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 30-Jun-2020

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements