• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
JensonBin
博客园    首页    新随笔    联系   管理    订阅  订阅

mssql 分割字符串,从左至右,从右至左

1.从左至右

declare @stt varchar(200)
declare @i int
declare @sttl varchar(100)
set @stt='0_123_456_789'
set @i=0
 while   @i<len(@stt)
  begin
    set   @i=charindex('_',@stt)
    set   @sttl=left(@stt,@i-1)
    set   @stt=right(@stt,len(@stt)-@i)

   --print @sttl
    print @stt
  end
输出:

123_456_789
456_789
789

但我想
789
456
123
0

2.从右至左


create table #tempta(id int identity(1,1),nid varchar(200))
declare @stt varchar(200)
declare @strl varchar(200)
declare @i int
declare @sttl varchar(100)

set @stt='0_123_456_789'
set @i=0
 while   @i<len(@stt)
  begin
    set   @i=charindex('_',@stt)
    set   @strl=left(@stt,@i-1) 
    set   @stt=right(@stt,len(@stt)-@i)
    insert into #tempta(nid) values(@strl)
  end

declare @nid varchar(200)
 declare open_cursor cursor for select nid from #tempta order by nid desc
    open open_cursor
    fetch next from open_cursor into  @nid
     while @@fetch_status=0               --循环取值
  begin
          print @nid
          fetch   next   from   open_cursor  into @nid     
         end
      close open_cursor
       deallocate open_cursor
   drop table #tempta

posted @ 2010-11-25 17:00  JensonBin  阅读(915)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3