当前位置: 代码迷 >> Sql Server >> sql语句,查询第n个空格在所在字符串中的位置(在该串中的位置),该怎么解决
  详细解决方案

sql语句,查询第n个空格在所在字符串中的位置(在该串中的位置),该怎么解决

热度:96   发布时间:2016-04-27 13:52:02.0
sql语句,查询第n个空格在所在字符串中的位置(在该串中的位置)
例如:62.98 242.17 74.51 253.72 5.11 184.27 82.29 261.47 71.69 250.90 59.70 238.89 58.83 238.01 28.70 207.85 106.46 285.65 62.56 241.71 
第17个空格在该串的位置,即第17个空格到起始字符的字符个数。这个sql语句怎么写?

------解决方案--------------------
SQL code
declare @str varchar(1000)declare @n intset @n=1select @str='62.98 242.17 74.51 253.72 5.11 184.27 82.29 261.47 71.69 250.90 59.70 238.89 58.83 238.01 28.70 207.85 106.46 285.65 62.56 241.71'while @n<=17beginselect @str=right(@str,len(@str)-charindex(' ',@str))select @[email protected]+1endprint @str
------解决方案--------------------
SQL code
declare @sIncludeParaMDL varchar(1000),@nPosi int,@nPosi1 int,@nPosi2 intset @sIncludeParaMDL='62.98 242.17 74.51 253.72 5.11 184.27 82.29 261.47 71.69 250.90 59.70 238.89 58.83 238.01 28.70 207.85 106.46 285.65 62.56 241.71 'select @nPosi =charindex(' ',@sIncludeParaMDL,1)select @nPosi1=1select @nPosi2=1while @nPosi>0begin        --这里是地几个空格    if(@nPosi2=18)    select   substring(@sIncludeParaMDL,1,@nPosi)        --这里是第17个到18之间的数    if(@nPosi2=18)    select   substring(@sIncludeParaMDL,@nPosi1,@[email protected])        --select @nPosi,@nPosi1,@nPosi2        select @nPosi1 = @nPosi+1,@[email protected]+1    select @nPosi =charindex(' ',@sIncludeParaMDL,@nPosi1)    end
  相关解决方案