我在SQL表中有一个电话号码,我想在电话号码为10位的地方添加前缀'0',但是,如果它小于10或大于10数字,则不需要0个前缀。
7863176061
7724269820
2088076157
1992762084
1318912
输出
07863176061
07724269820
02088076157
01992762084
1318912
尝试此
SELECT CASE LEN(Num) WHEN 10 THEN '0'+cast(Num as varchar(11)) ELSE Num END AS Num
尝试以下:
select
case when len(yourcolumn) =10
then '0'+ yourcolumn
else yourcolumn end as column
from yourtable
使用 CASE
表达式检查长度。
查询
select
case when len([phone_number]) = 10
then '0' + cast([phone_number] as varchar(20))
else cast([phone_number] as varchar(20)) end
from [your_table_name];
在此处找到演示
如果列数据类型为bigint,则必须将电话号码列列为VARCHAR。否则,您可以在上述查询中排除铸件部分。
我认为这会很快,
select
'0' + cast([phone_number] as varchar(20))
from [your_table_name]
where len([phone_number]) = 10 ;