Skip to content
Loading
  • Shahriar Nikkhah
    +1
    Just changed the CASE statement to cover scenarios like
     
    ('A$*$***$*$**$**$*',1,'A',NUll),
    ('*$*$***$*$**$**$Z',7,'Z',NUll),
     
     
    1. SELECT  *  
    2.     ,   StatusNew = CASE    --WHEN [Position] = 0 THEN 'Not Match Characters'  
    3.                             --WHEN [CharecterAfter] <> '$' THEN 'Not Match Characters'  
    4.                             --WHEN [CharecterBefore] <> '$' THEN 'Not Match Characters'  
    5.                             WHEN [Position] > 1 AND [CharecterAfter] = '$' AND [CharecterBefore] = '$' THEN '       Match Characters'  
    6.                             WHEN [Position] = 1 AND [CharecterAfter] = '$' THEN '       Match Characters'  
    7.                             WHEN [Position] = LEN(SignatureKey) AND [CharecterBefore] = '$' THEN '       Match Characters'  
    8.                             ELSE 'Not Match Characters'  
    9.                     END   
    10. FROM    (   SELECT  *  
    11.                 ,   Position = CHARINDEX (PortionKeyNew , SignatureKey)  
    12.                 ,   CharecterBefore = SUBSTRING(SignatureKey ,CHARINDEX (PortionKeyNew , SignatureKey) - 1 ,1)  
    13.                 ,   CharecterAfter = SUBSTRING(SignatureKey ,CHARINDEX (PortionKeyNew , SignatureKey) + LEN(PortionKeyNew) ,1 )  
    14.   
    15.             FROM    (   SELECT  *  
    16.                             ,   PortionKeyNew = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(PortionKey , '0' , '*') , '1' , '*') , '2' , '*') , '3' , '*') , '4' , '*') , '5' , '*') , '6' , '*') , '7' , '*') , '8' , '*') , '9' , '*')  
    17.                         FROM    #Ref  
    18.                     ) AS itbl   
    19.         ) AS MT  
    +1
  • Shahriar Nikkhah
    +1
    You may need to add more scenarios to the CASE statement
     
    1. WHEN [Position] > 1 AND [CharecterAfter] = '$' AND [CharecterBefore] = '$' THEN '       Match Characters'  
    2. WHEN [Position] = 1 AND [CharecterAfter] = '$' THEN '       Match Characters'  
    3. WHEN [Position] = LEN(SignatureKey) AND [CharecterBefore] = '$' THEN '       Match Characters'  
    4. WHEN LEN(SignatureKey)  = [Position] + LEN(PortionKeyNew) - 1 AND [CharecterBefore] = '$' THEN '       Match Characters'  
    5. ELSE 'Not Match Characters'  
     
    +1
  • Shahriar Nikkhah
    +1
    this should be final i hope
    1. SELECT  *  
    2.     --,  LEN(SignatureKey) AS LenSignatureKey  
    3.     ,   StatusNew = CASE    --WHEN [Position] = 0 THEN 'Not Match Characters'  
    4.                             --WHEN [CharacterAfter] <> '$' THEN 'Not Match Characters'  
    5.                             --WHEN [CharacterBefore] <> '$' THEN 'Not Match Characters'  
    6.                             WHEN [Position] > 1 AND [CharacterAfter] = '$' AND [CharacterBefore] = '$' THEN '       Match Characters'  
    7.                             WHEN [Position] = 1 AND [CharacterAfter] = '$' THEN '       Match Characters'  
    8.                             --WHEN [Position] = LEN(SignatureKey) AND [CharacterBefore] = '$' THEN '       Match Characters'  
    9.                             WHEN LEN(SignatureKey)  = [Position] + LEN(PortionKeyNew) - 1 AND [CharacterBefore] = '$' THEN '       Match Characters'  
    10.                             ELSE 'Not Match Characters'  
    11.                     END   
    12. FROM    (   SELECT  *  
    13.                 ,   Position = CHARINDEX (PortionKeyNew , SignatureKey)  
    14.                 ,   CharacterBefore = SUBSTRING(SignatureKey ,CHARINDEX (PortionKeyNew , SignatureKey) - 1 ,1)  
    15.                 ,   CharacterAfter = SUBSTRING(SignatureKey ,CHARINDEX (PortionKeyNew , SignatureKey) + LEN(PortionKeyNew) ,1 )  
    16.   
    17.             FROM    (   SELECT  *  
    18.                             ,   PortionKeyNew = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(PortionKey , '0' , '*') , '1' , '*') , '2' , '*') , '3' , '*') , '4' , '*') , '5' , '*') , '6' , '*') , '7' , '*') , '8' , '*') , '9' , '*')  
    19.                         FROM    #Ref  
    20.                     ) AS itbl   
    21.         ) AS MT  
     
    +1
  • Shahriar Nikkhah
    +1
    test it with 
    1. insert into #Ref(SignatureKey,GroupId,PortionKey,status)    
    2. values    
    3. ('A$*$***$*$**$**$*',1,'A',NUll),  
    4. ('*$*$***$*$**$**$Z',7,'Z',NUll),  
    5. ('A**$*$***$*$**$**$*',1,'A12',NUll),  
    6. ('*$*$***$*$**$**$**Z',7,'12Z',NUll),  
    7. ('**P$*$***$*$**$**$*',1,'12P',NUll),  
    8. ('*$*$***$*$**$**$D**',7,'D12',NUll),  
    9. ('*$*$***$C$**$**$*',4,'C',NUll),   
    10. ('*$*$C$***$**$**$*',3,'s',NUll),    
    11. ('*$*$*$FG$*$**$*',4,'F',NUll),    
    12. ('*$*$*$***$*$D$*',6,'D',NUll),    
    13. ('*$t**$*$***$***$**$*',2,'t12',NUll),    
    14. ('*$**$*$***$**t$**$*',5,'12t',NUll)   
    +1