[英]How to split the numeric values string split into digits in SQL Server
ID PHONE
1 9701245929
2 66663333
3 9701245931
4 9701245932
5 26668888
6 48228899
7 88229933
OUTPUT: OUTPUT:
ID PHONE
1 9701 245 929
2 6666 3333
3 9701 245 931
4 9701 245 932
5 2666 8888
6 4822 8899
7 8822 9933
You can use a query like below See working demo 您可以使用如下查询方式查看工作演示
select id,
phone=case
when
len(phone)=10
then
FORMAT(cast(phone as bigint), '### ### ###')
when
len(phone)=8
then
FORMAT(cast(phone as bigint), '#### ####')
end
from t;
You could use case and build the string or format as others have suggested. 您可以使用大小写并按照其他人的建议构建字符串或格式。
SELECT
id
,CASE
WHEN LEN(phone) = 10 THEN SUBSTRING(phone, 1, 4) + ' ' + SUBSTRING(phone, 5, 3) + ' ' + SUBSTRING(phone, 8, 3)
WHEN LEN(phone) = 8 THEN LEFT(phone, 4) + ' ' + RIGHT(phone, 4)
END
FROM YourTable
You need format()
: 您需要format()
:
select format(PHONE, '### ### ###') as Phone
from table t;
Edit
: You can use case expression for conditional formatting Edit
:您可以将案例表达式用于条件格式
select *, (case when len(Phone) = 8
then format(Phone, '#### ####')
else format(Phone, '#### ### ###')
end) as Phone
from table t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.