~~~~~~~~~字段截取的问题? 100分求~~~~~~~

wendi 2006-06-08 11:12:25
假设现在一个表中有三个字段,表结构如下:

A | B C
813zx-01-5/9 |
813zx-01-5/12 |

我如何通过一个统一的语句将字段A中的两条记录中的字符5截取出来放到B中,第一条记录中的最后的数字 9 和第二条记录中的 12 截取出来放到字段C中。

谢谢!!

...全文
172 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwh999 2006-06-08
  • 打赏
  • 举报
回复
update 表名稱 set b=Substring(A,charindex('/',A)-1,1),c=Substring(A,charindex('/',A)+1,len(A))

對上面語句的解釋:

對于B字段,使用charindex來查找'/'的位數,然後使用substring函數根據這個位置取出一個字符
注意: 此處使用的是charindex('/',A)-1

對于C字段的處理基本上一樣,不過其取出的位數=不定,有可能1位,有可能2位,這時可以用len(A)來處理,如果數值太大,SUBSTRING函數最多會取至末尾
xeqtr1982 2006-06-08
  • 打赏
  • 举报
回复
declare @T table(A varchar(20),B varchar(20),C varchar(20))
insert into @t(A) select '813zx-01-5/9'
union all select '813zx-01-5/12'
union all select '813zx-01-51/12'
union all select '813zx-01-53331/31312'

update @t set b=left(right(A,charindex('-',reverse(A))-1),charindex('/',right(A,charindex('-',reverse(A))-1))-1),
c=Substring(right(A,charindex('-',reverse(A))-1),charindex('/',right(A,charindex('-',reverse(A))-1))+1,len(right(A,charindex('-',reverse(A))-1)))

select * from @t
lxzm1001 2006-06-08
  • 打赏
  • 举报
回复
update tablename set b=substring(A,10,1),c=right(a,len(a)-charindex('/',a))
xeqtr1982 2006-06-08
  • 打赏
  • 举报
回复
declare @T table(A varchar(20),B varchar(20),C varchar(20))
insert into @t(A) select '813zx-01-5/9'
union all select '813zx-01-5/12'

update @t set b=Substring(A,charindex('/',A)-1,1),c=Substring(A,charindex('/',A)+1,len(A)) from @t
select * from @t
lxzm1001 2006-06-08
  • 打赏
  • 举报
回复
update tablename set c=right(a,len(a)-charindex('/',a))
lxzm1001 2006-06-08
  • 打赏
  • 举报
回复
update tablename set b=substring(A,10,1)

34,838

社区成员

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

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