更新列的问题。

hcl8260 2008-12-22 01:47:20
刚学SQL,请大家帮我看看。
create table #a
(
fnumber varchar(255),
fshortNumber varchar(255)
)

Insert into #a
Select 'A01' union all
Select 'A01.01' union all
Select 'A01.01.001' union all
Select 'A01.002' union all
Select 'A01.002.01.0004'

希望将fshortNumber 更新成为最后一个'.'后的值。没‘.’就等于fnumber列。谢谢大家!

结果是select * from #a
A01 A01
A01.01 01
A01.01.001 001
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
windfeel2008 2008-12-22
  • 打赏
  • 举报
回复

create table #a
(
fnumber varchar(255),
fshortNumber varchar(255)
)

Insert into #a
Select 'A01','' union all
Select 'A01.01','' union all
Select 'A01.01.001','' union all
Select 'A01.002','' union all
Select 'A01.002.01.0004',''

update #a set fshortNumber=(Case CharIndex('.',rTrim(fnumber)) When 0 then rTrim(fnumber) Else Right(rTrim(fnumber),CharIndex('.',Reverse(rTrim(fnumber)))-1) End)

liangCK 2008-12-22
  • 打赏
  • 举报
回复
create table #a 
(
fnumber varchar(255),
fshortNumber varchar(255)
)

Insert into #a(fnumber)
Select 'A01' union all
Select 'A01.01' union all
Select 'A01.01.001' union all
Select 'A01.002' union all
Select 'A01.002.01.0004'

UPDATE #a SET
fshortNumber=RIGHT(fnumber,CHARINDEX('.',REVERSE('.'+fnumber))-1)

select * from #a

drop table #a
liangCK 2008-12-22
  • 打赏
  • 举报
回复
UPDATE #a SET 
fshortNumber=RIGHT(fnumber,CHARINDEX(REVERSE(fnumber+'.'))-1)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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