ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.3k

How to rewrite sql query after where condition with another query ?

May 15 2021 6:20 PM

I work on sql server I need to rewrite statement after where statement so how to rewrite
I don't understand what is writing after where
so can you help me how to write it alternative
  1. select top 1 *  
  2. FROM  
  3. dbo.GlobalPartNumberPortions Po WITH(NOLOCK)  
  4. INNER JOIN dbo.GlobalPartNumber GOl WITH(NOLOCK) ON GOl.GlobalPnId = Po.GlobalPnId AND Po.GroupId = 1  
  5. INNER JOIN Parts.Nop_PartsFamily pf WITH(NOLOCK) ON GOl.FamilyId = pf.PartFamilyID  
  6. INNER JOIN dbo.GlobalPartNumberPortions Po2 WITH(NOLOCK) ON GOl.GlobalPnId = Po2.GlobalPnId AND Po2.GroupId = 2  
  7. WHERE @PartNumber LIKE CONCAT(LTRIM(RTRIM(CASE WHEN Po.PortionKey=N'Blank' THEN ''  
  8. WHEN CHARINDEX('[', PO.PortionKey) >0 then replace(PO.PortionKey,N'[',N'[[')  
  9. ELSE Po.PortionKey END))  
  10. ,LTRIM(RTRIM(CASE WHEN Po2.PortionKey=N'Blank' THEN ''  
  11. WHEN CHARINDEX('[', PO2.PortionKey) >0 then replace(PO2.PortionKey,N'[',N'[[')  
  12. ELSE Po2.PortionKey END))  
  13. '%')  
  14.   
  15. what i need it rewrite statement as below :  
  16.   
  17. WHERE @PartNumber LIKE CONCAT(LTRIM(RTRIM(CASE WHEN Po.PortionKey=N'Blank' THEN ''  
  18. WHEN CHARINDEX('[', PO.PortionKey) >0 then replace(PO.PortionKey,N'[',N'[[')  
  19. ELSE Po.PortionKey END))  
  20. ,LTRIM(RTRIM(CASE WHEN Po2.PortionKey=N'Blank' THEN ''  
  21. WHEN CHARINDEX('[', PO2.PortionKey) >0 then replace(PO2.PortionKey,N'[',N'[[')  
  22. ELSE Po2.PortionKey END))  
  23. '%')  
really i don't understand what after where condition so can you help me to understand what written after where
condition
or
rewrite it with another syntax or logic ?
this actually i need to rewrite it
  1. WHERE @PartNumber LIKE CONCAT(LTRIM(RTRIM(CASE WHEN Po.PortionKey=N'Blank' THEN ''    
  2. WHEN CHARINDEX('[', PO.PortionKey) >0 then replace(PO.PortionKey,N'[',N'[[')    
  3. ELSE Po.PortionKey END))    
  4. ,LTRIM(RTRIM(CASE WHEN Po2.PortionKey=N'Blank' THEN ''    
  5. WHEN CHARINDEX('[', PO2.PortionKey) >0 then replace(PO2.PortionKey,N'[',N'[[')    
  6. ELSE Po2.PortionKey END))    
  7. '%')   
portion key is RXQ6R8
@PartNumber is RXQ6R8M2WSA-1020S

Answers (1)