CONVERT() फंक्शन या रेगुलर एक्सप्रेशन का उपयोग करें। CONVERT() मेथड एक वैल्यू को एक डेटा टाइप से दूसरे में कन्वर्ट करता है। यह अंततः हमारे लिए डिजिट्स फेच करेगा। आइए एक उदाहरण देखते हैं।
सबसे पहले, हम एक टेबल बनाएंगे।
mysql> create table textIntoNumberDemo -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.47 sec)
कुछ रिकॉर्ड्स इन्सर्ट करना।
mysql> insert into textIntoNumberDemo values('John-11');
Query OK, 1 row affected (0.11 sec)
mysql> insert into textIntoNumberDemo values('John-12');
Query OK, 1 row affected (0.17 sec)
mysql> insert into textIntoNumberDemo values('John-2');
Query OK, 1 row affected (0.11 sec)
mysql> insert into textIntoNumberDemo values('John-4');
Query OK, 1 row affected (0.14 sec)
सभी रिकॉर्ड्स डिस्प्ले करने के लिए।
mysql> select * from textIntoNumberDemo;
आउटपुट यहाँ है।
+---------+ | Name | +---------+ | John-11 | | John-12 | | John-2 | | John-4 | +---------+ 4 rows in set (0.00 sec)
डिजिट फेच करने का सिंटैक्स।
SELECT yourColumnName, CONVERT(SUBSTRING_INDEX(yourColumnName, '-', -1), UNSIGNED INTEGER) AS yourVariableName FROM yourTableName ORDER BY yourVariableName;
क्वेरी निम्न है।
mysql> SELECT Name, CONVERT(SUBSTRING_INDEX(Name, '-', -1), UNSIGNED INTEGER) AS MyNumber -> FROM textIntoNumberDemo -> ORDER BY MyNumber;
आउटपुट यहाँ है।
+---------+----------+ | Name | MyNumber | +---------+----------+ | John-2 | 2 | | John-4 | 4 | | John-11 | 11 | | John-12 | 12 | +---------+----------+ 4 rows in set (0.00 sec)