开发者

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

开发者 https://www.devze.com 2023-02-25 09:47 出处:网络
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\':

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

精彩评论

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