MySQL automatic string to integer casting in WHERE clause to fetch a specific id

MySQLMySQLi Database

If the string begins with integer then it converts the string to integer, otherwise it won’t. Let us first create a −

mysql> create table DemoTable1390
   -> (
   -> StudentId varchar(20)
   -> );
Query OK, 0 rows affected (0.93 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1390 values('563_John');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1390 values('1001_Carol_Taylor');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1390 values('David_Miller_789');
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable1390 values('456_AdamSmith');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select −

mysql> select * from DemoTable1390;

This will produce the following output −

+-------------------+
| StudentId         |
+-------------------+
| 563_John          |
| 1001_Carol_Taylor |
| David_Miller_789  |
| 456_AdamSmith     |
+-------------------+
4 rows in set (0.00 sec)

Following is the query for automatic string to integer casting in where clause to fetch a specific −

mysql> select * from DemoTable1390 where StudentId=456;

This will produce the following output −

+---------------+
| StudentId     |
+---------------+
| 456_AdamSmith |
+---------------+
1 row in set, 4 warnings (0.02 sec)
raja
Published on 11-Nov-2019 10:21:47
Advertisements