[英]Replacing strings with integers in SQL Server
I'm trying to replace any phone number in a column that may consist with the letter o instead of the number 0. Is there any way I can do this?我正在尝试替换可能包含字母 o 而不是数字 0 的列中的任何电话号码。有什么办法可以做到这一点?
For example: a Phone
column that accepts NVARCHAR
and there are multiple inputs of numbers like this:例如:接受
NVARCHAR
的Phone
列,并且有多个数字输入,如下所示:
1-800-9o6o
(962)47l-9o8o
(472)1o4-7o91
by multiple I mean 80+多个我的意思是 80+
With a simple replace statement.用一个简单的替换语句。 https://msdn.microsoft.com/en-us/library/ms186862.aspx
https://msdn.microsoft.com/en-us/library/ms186862.aspx
Replace(YourColumn, 'o', '0')
You can use replace function:您可以使用替换功能:
replace(phone_number, 'o', '0')
If you are selecting, then use:如果您正在选择,请使用:
select replace(phone_number, 'o', '0') from t;
If you need to update the table:如果需要更新表:
update t
set phone_number = replace(phone_number, 'o', '0')
where phone_number like '%o%';
Just use replace()
:只需使用
replace()
:
update t
set phone = replace(phone, 'o', '0')
where phone like '%o%';
Given your examples, you might also want to change the lower-case "l" to 1:鉴于您的示例,您可能还想将小写的“l”更改为 1:
update t
set phone = replace(replace(phone, 'o', '0'), 'l', '1')
where phone like '%[ol]%';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.