哎。。。。!字符串啊

ggs_ddup 2008-01-17 11:36:38
我想在字符串如:2,3,5,11,26 中查找
26的位置索引。
用charindex得到的结果是10
我想要得到的索引为5
请赐教
...全文
71 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggs_ddup 2008-03-03
  • 打赏
  • 举报
回复
谢谢!问题已解决
areswang 2008-01-18
  • 打赏
  • 举报
回复
函数,学习!
-狙击手- 2008-01-18
  • 打赏
  • 举报
回复

create proc f_search
@keyword int,
@text nvarchar(2000),
@ret int out
as
begin
create table ##(id int identity(1,1),skey int)
declare @s varchar(8000)
set @s = 'insert ##(skey) select '+replace(@text,',',' union select ')
exec(@s)
select @ret = id from ## where skey = @keyword
drop table ##
end
go
declare @i int
exec f_search 26,'2,3,5,11,26',@I out
select @I
drop proc f_search


/*

-----------
5

(所影响的行数为 1 行)

*/
areswang 2008-01-18
  • 打赏
  • 举报
回复
select (len('2,3,5,11,26')-len(replace('2,3,5,11,26',',','')))+1
JL99000 2008-01-18
  • 打赏
  • 举报
回复
declare @s varchar(100),@count int
set @s='2,3,5,11,26'
set @count=0
while(charindex(',',@s)>0)
begin
set @s=substring(@s,(charindex(',','2,3,5,11,26'))+1,len(@s))
set @count=@count+1
end
select @count
--
很简单,接分了
areswang 2008-01-17
  • 打赏
  • 举报
回复
select len('2,3,5,11,26')-len(replace('2,3,5,11,26',',',''))+1
--------------------
这样也可以实现,查出逗号的个数加1即为26的位置

34,591

社区成员

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

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