Computer >> Computer tutorials >  >> Programming >> MySQL

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('[email protected]');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('[email protected]');
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             |
+-------------------+
| [email protected] |
| [email protected]  |
+-------------------+
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)