How to find specific VALUE to specific STRING location in different strings

后端 未结 2 1709
梦如初夏
梦如初夏 2020-12-20 09:52

I have a sting like this
,x,x,y,x,x,O,x,y
that matches to its values in an other sting like this
0~1~b~~z~XY~1~7.
The value \"O\" can switch its position in

2条回答
  •  佛祖请我去吃肉
    2020-12-20 10:43

    Grab a copy of DelimitedSplit8K then you can do this:

    DECLARE @string1 VARCHAR(1000) = ',x,x,y,x,x,O,x,y',
            @string2 VARCHAR(1000) = '0~1~b~~z~XY~1~7';
    
    DECLARE @search VARCHAR(1000) = 'O'; -- best as a variable/parameter
    
    SELECT *
    FROM dbo.delimitedSplit8K(@string2,'~') AS s
    WHERE s.itemNumber = 
    (
      SELECT TOP (1) s2.itemNumber -- TOP (1) until we know about dupicates
      FROM   dbo.delimitedSplit8K(@string1,',') AS s2
      WHERE  s2.item = @search
    )-1;
    

    Returns:

    ItemNumber           Item
    -------------------- -------
    6                    XY
    

提交回复
热议问题