【发布时间】:2013-05-15 18:17:48
【问题描述】:
我需要一种更好的方法来替换字符串中的非数字字符。
我有这样的电话号码 (888) 488-6655 888-555-8888 哈哈哈哈哈哈
所以我可以使用简单的替换函数返回一个干净的字符串,但我正在寻找一种更好的方法,可能是使用表达式函数来替换任何非数字值。像空格斜杠,反斜杠,引号......任何无数值
这是我当前的查询
SELECT
a.account_id,
REPLACE(REPLACE(REPLACE(REPLACE(t.phone_number, '-', ''), ' ', ''), ')', ''),'(','') AS contact_number,
IFNULL(t.ext, '') AS extention,
CASE WHEN EXISTS (SELECT number_id FROM contact_numbers WHERE main_number = 1 AND account_id = a.account_id) THEN 0 ELSE 1 END AS main_number,
'2' AS created_by
FROM cvsnumbers t
INNER JOIN accounts a ON a.company_code = t.company_code
WHERE REPLACE(REPLACE(REPLACE(REPLACE(t.phone_number, '-', ''), ' ', ''), ')', ''),'(','') NOT IN(SELECT contact_number FROM contact_numbers WHERE account_id = a.account_id)
AND LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(t.phone_number, '-', ''), ' ', ''), ')', ''),'(','') ) = 10
如何更改我的查询以使用 REGEX 替换非数字值。
谢谢
【问题讨论】:
-
正如所有其他包含
mysql和regex的问题所表明的那样,这不是原生MySQL。你could use this.