SQL选择CASE-WHEN – 如何从电话号码中删除格式。

huangapple go评论65阅读模式
英文:

SQL Select CASE-WHEN - How to remove formatting from telephone numbers

问题

SELECT语句创建一个表,其中包含多个字段。我使用CASE/WHEN来更改一些值,但我不知道如何格式化结果。

SELECT DISTINCT field1, field2, field3, field4,
CASE meta_value
    WHEN 'male' THEN 'M'
    WHEN 'female' THEN 'F'
    -- 其他情况的处理
END
-- 其他SELECT语句部分

返回的电话号码可能带有其他字符,我希望去除,例如:

(404) 202-2039
404-202-2039
1-404-202-2039

我想要返回只有数字,没有空格、连字符、括号等其他字符的电话号码:

4042022039

有什么好的方法可以实现这个吗?

英文:

I have a SQL Select statement that creates a table from a number of fields. I'm using the CASE/WHEN to change some of the values but I'm stuck on how to format the results.

SELECT DISTINCT field1, field2, field3, field4,
CASE meta_value WHEN 'male' then 'M' WHEN 'female' THEN 'F' etc.

The telephone numbers that are returned may have other characters that I would like to remove, i.e.:

(404) 202-2039
404-202-2039
1-404-202-2039

I would like to return only the numbers with no spaces or other characters, hyphens, parentheses, etc:

4042022039

Any ideas on how to best accomplish this?

答案1

得分: 1

如果您正在运行MySQL 8.0,则可以使用regexp_replace()函数:

regexp_replace(phone_number, '[^0-9]', '') clean_phone_number
英文:

If you are running MySQL 8.0, you can use regexp_replace():

regexp_replace(phone_number, '[^0-9]', '') clean_phone_number

huangapple
  • 本文由 发表于 2020年10月23日 22:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64502115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定