简体   繁体   English

仅从sql中的地址列获取数字

[英]Fetch numbers only from an address column in sql

Actually in my case I need to select street number from a address string, which means if the string is ' 1234 dummy789 road ', I only want to get ' 1234 ', not ' 1234789 ' Another example is ' Plot 111 dummy 1220 ' then i want only ' 111 '. 实际上,在我的情况下,我需要从地址字符串中选择街道号码,这意味着如果字符串是“ 1234 dummy789 road ”,我只想获取“ 1234 ”,而不是“ 1234789 ”。另一个示例是“ Plot 111 dummy 1220 ”,我只想要' 111 '。 and if the string is ' 111/2 dummy ' then i want to get ' 111/2 ' 如果字符串是“ 111/2 dummy ”,那么我想得到“ 111/2

I tried following: 我尝试了以下操作:

SELECT CASE WHEN substr(address , 1, 1) between '0' and '9'
              THEN substr(address , 1, 1)
            ELSE 'False'
       END as add
from test
    <?php
        $ab = "1225584454 red 1555  blue";
        $result = explode(" ", $ab, 2);
        print_r($result);
    ?>

值的屏幕截图

in this case this will gives you first string in your variable. 在这种情况下,这将为您提供变量中的第一个字符串。

Assuming that you have civil number followed by space and street name I would suggest the following: Put WHERE statement with REGEXP to get those, which start with digit-followed-by-space. 假设您有民用号码,后跟空格和街道名称,那么我将提出以下建议:将WHERE语句与REGEXP一起获取,以空格后面的数字开头。 And in returned field get only numeric portion with substring. 并且在返回的字段中仅获取带有子字符串的数字部分。

Something like this: 像这样:

SELECT SUBSTRING(address, 0,  LOCATE(' ', address)) FROM items WHERE `address` REGEXP '^[0-9]+ '>0;

Correction: 更正:

SELECT TRIM(LEFT(address, LOCATE(' ', address))) FROM items WHERE `address` REGEXP '^[0-9]+ '>0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM