开发者

working of charindex,cross apply and substring in single query of sql

I have a query as follows to display @prodid= ''1,2,10,4,5,6,7,8,13,16,17,3'' from the st开发者_StackOverflow中文版ring 'dbo.proudction @prodid= ''1,2,10,4,5,6,7,8,13,16,17,3'' ,@stock= 0':

declare @T table(Col1 varchar(100))
insert into @T values ('dbo.proudction @prodid= ''1,2,10,4,5,6,7,8,13,16,17,3'' ,@stock= 0')

select 
  substring(Col1, Start, Stop-Start)
from @T
  cross apply 
    (select charindex('''', Col1)+1) c1(Start)
  cross apply
    (select charindex('''', Col1, Start)) c2(Stop)

Can you help me to understand the working of this query, please ?


The first cross apply use charindex locate the first quote in the string. The second cross apply locates the second quote in the string. Then substring will extract the text between the first quote and the second quote.

Result

1,2,10,4,5,6,7,8,13,16,17,3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜