简体   繁体   中英

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 '. and if the string is ' 111/2 dummy ' then i want to get ' 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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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