开发者

Return substring up to first digit

开发者 https://www.devze.com 2023-04-09 23:55 出处:网络
I am trying to group rows by a field that either开发者_如何学Go has the pattern [:alpha:][:digit:].* or [:alpha:][:alpha:][:digit:].* by the substring up to but excluding the digit. i.e. the returned

I am trying to group rows by a field that either开发者_如何学Go has the pattern [:alpha:][:digit:].* or [:alpha:][:alpha:][:digit:].* by the substring up to but excluding the digit. i.e. the returned substring will either have one letter, or two.

I am thinking something along the lines of:

SELECT
  LEFT(postcode,IF(ISDIGIT(postcode,2),1,2)) AS area,
  COUNT(*) AS num
FROM addresses
GROUP BY
  LEFT(postcode,IF(ISDIGIT(postcode,2),1,2))

Except of course there is no ISDIGIT() function.

I was also thinking of something similar to LEFT(postcode,POSITION_REGEX("\d" IN postcode)) but obviously that doesn't exist either :-/

Database server is running MySQL 4.1.24

Upgrading to 5.0 is possible but would require downtime and hasn't been done yet as it hasn't been necessary so far.


SELECT
    LEFT(postcode, IF(postcode REGEXP '^.[[:digit:]]', 1, 2)) AS area,
    COUNT(*) AS num
FROM addresses
GROUP BY area
0

精彩评论

暂无评论...
验证码 换一张
取 消