[英]Extract a set of digits from a string column in MySQL
我正在嘗試從MySQL的字符串列中提取固定長度的數字,我該如何去做? 通常的like和regexp無法正常工作,因此可以肯定需要一個函數,但是不確定從哪里開始以及如何編寫一個函數。
請指教。
固定長度的數字是10位數字,並且是字符串列的示例:
'SGD0.00 AUTOMSG:'0179187381' is near Esso @ 23:59.'
請注意,字符串中10位數字的位置會有所不同。
簡單:
Just use: SUBSTR(str,x,y)
where:- str -> String,
x -> position,
y -> length
示例: SELECT SUBSTR('ABCDEFG',2,3); -> BCD
有關更多信息,請訪問以下鏈接: http : //www.w3resource.com/mysql/string-functions/mysql-substring-function.php
mysql> select substr("'SGD0.00 AUTOMSG:'0179187381' is near Esso @ 23:59.'", 19,10);
+-----------------------------------------------------------------------+
| substr("'SGD0.00 AUTOMSG:'0179187381' is near Esso @ 23:59.'", 19,10) |
+-----------------------------------------------------------------------+
| 0179187381 |
+-----------------------------------------------------------------------+
1 row in set (0.48 sec)
您可以這樣嘗試:
SELECT SUBSTRING('Quadratically',5);
語法: SUBSTRING(str,pos)
有關更多信息: http : //dev.mysql.com/doc/refman/5.0/zh-CN/string-functions.html#function_substring
如果10位數字始終以:'
開頭,則可以使用:
select substr(column_name, locate(":'", column_name) + 2, 10) from table_name
(假設:'
不會出現在字符串中數字之前的任何其他位置)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.