Update语句如何写

luohy527 2005-06-15 10:44:01
有这个一个问题请教大家望多多指点;
name tel
A 05515301598
B 01047895323
C 05515478953
D 01025165646
...     ....    

要求用SQL语句实现如下结果:
name tel
A 0551-5301598
B 010-47895323
C 0551-5478953
D 010-25165646
...     .... 

在线等……
...全文
289 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
luohy527 2005-06-15
  • 打赏
  • 举报
回复
OK
问题已解决,谢谢各位参于。
luohy527 2005-06-15
  • 打赏
  • 举报
回复
自顶
luohy527 2005-06-15
  • 打赏
  • 举报
回复
Luohy527 010-5319681
Luohy527 010-3196872
Luohy527 0551-319681
Luohy527 0551-319683
Luohy527 0551-319684
Luohy527 0551-319686
Luohy527 0551-319689
前面现在有点像
但是还少一位。原01015319681 结果:010-5319681
zv008 2005-06-15
  • 打赏
  • 举报
回复
Update tablename set
tel=case left(tel,2)
when '01' then left(tel,3)+'-'+right(tel,len(tel)-3)
when '02' then left(tel,3)+'-'+right(tel,len(tel)-3)
else left(tel,4)+'-'+right(tel,len(tel)-4)
end
tx1icenhe 2005-06-15
  • 打赏
  • 举报
回复
怎么会这样?

改成以下试试(你的sql server是不是没打补丁sp3):

update tablename
set tel=case when tel like '01%' or tel like '02%' then left(tel,3)+'-'+right(tel,len(rtrim(tel))-3)
else left(tel,4)+'-'+right(tel,len(rtrim(tel))-4)
end
luohy527 2005-06-15
  • 打赏
  • 举报
回复
insert into tel
select 'Luohy527','05515319681' union
select 'Buohy527','05515319683' union
select 'Cuohy527','05515319684' union
select 'Duohy527','01015319681' union
select 'Euohy527','05515319686' union
select 'Fuohy527','01053196872' union
select 'Guohy527','05515319689'
 用update tablename
set tel=case when tel like '01%' or tel like '02%' then left(tel,3)+'-'+right(tel,len(tel)-3)
else left(tel,4)+'-'+right(tel,len(tel)-4)
end
后有如下结果:
Buohy527 0551-319683
Cuohy527 0551-319684
Duohy527 010-5319681
Euohy527 0551-319686
Fuohy527 010-3196872
Guohy527 0551-319689
Luohy527 0551-319681
中间的'05515319681' 53换成了“—”
zhangzs8896 2005-06-15
  • 打赏
  • 举报
回复
同意楼上
tx1icenhe 2005-06-15
  • 打赏
  • 举报
回复
update tablename
set tel=case when tel like '01%' or tel like '02%' then left(tel,3)+'-'+right(tel,len(tel)-3)
else left(tel,4)+'-'+right(tel,len(tel)-4)
end


27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧