gpt4 book ai didi

mysql - 如何替换表格列中的所有特殊字符和空格?

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:08 25 4
gpt4 key购买 nike

我有一个名为“contacts”的表,其中包含以下字段

id | phone_mobile

1 | 9988011223
2 | 00-12-123456
3 | 91-8876980987
4 | (91) 88990099777

我想要一个将返回以下输出的选择查询

id | phone_mobile

1 | 9988011223
2 | 0012123456
3 | 918876980987
4 | 9188990099777

最佳答案

对于一组已知的字符,您可以使用 replace() 函数将某些内容链接为

mysql> select replace(replace(replace('00-12-123456','-',''),'(',''),')','');
+----------------------------------------------------------------+
| replace(replace(replace('00-12-123456','-',''),'(',''),')','') |
+----------------------------------------------------------------+
| 0012123456 |
+----------------------------------------------------------------+

所以你的情况可能是

select 
id,
replace(replace(replace(replace(phone_mobile,'-',''),'(',''),')',''),' ','') as phone_mobile
from table_name

如果要替换的字符列表很长,那么最好使用应用程序级别来完成这项工作,因为替换链会变得非常困惑。

关于mysql - 如何替换表格列中的所有特殊字符和空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26968915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com