Sql server 在SQL中解析多个筛选器
我在解析以下形式的存储过程参数时遇到问题:Sql server 在SQL中解析多个筛选器,sql-server,string,Sql Server,String,我在解析以下形式的存储过程参数时遇到问题: declare @S varchar(100) set @S = '4=2,24=1534' 问题是: select cast(idx as varchar(100)) 'idx' , value , SUBSTRING(value, 1, charindex(value, '=')+1) 'first' , SUBSTRING(value, charindex(value, '=')
declare @S varchar(100)
set @S = '4=2,24=1534'
问题是:
select
cast(idx as varchar(100)) 'idx'
, value
, SUBSTRING(value, 1, charindex(value, '=')+1) 'first'
, SUBSTRING(value, charindex(value, '=')+1, LEN(value)-charindex(value, '=')-1) 'second'
from Common.SplitToTable(@S, ',') -- returns (idx int, value varchar(max))
where len(value) > 0
但我得到的结果是:
idx value first second
0 4=2 4 4=
1 24=1534 2 24=153
以下是我所期望的:
idx value first second
0 4=2 4 2
1 24=1534 2 1534
帮助?charindex的参数是向后的(首先是您要查找的字符串),并相应地调整长度:
select
cast(idx as varchar(100)) 'idx'
, value
, SUBSTRING(value, 1, charindex('=', value)-1) 'first'
, SUBSTRING(value, charindex('=', value)+1, LEN(value)-charindex('=',value)) 'second'
from Common.SplitToTable(@S, ',') -- returns (idx int, value varchar(max))
where len(value) > 0