Posted By

keidash on 07/09/13


Tagged

sql case where


Versions (?)

CASE IN WHERE SENTENCE


 / Published in: SQL
 

How to get a query based on case/when sintax, on WHERE statement (Treated as boolean expresion)

  1. SET NOCOUNT ON
  2. Declare @Sede varchar(10)
  3.  
  4. SET @Sede = 'TD'
  5.  
  6.  
  7. Declare @listas TABLE(Origen varchar(2))
  8.  
  9. INSERT @listas VALUES('AF')
  10. INSERT @listas VALUES('CP')
  11. INSERT @listas VALUES('LL')
  12. INSERT @listas VALUES('EC')
  13. INSERT @listas VALUES('PE')
  14. INSERT @listas VALUES('ZZ')
  15.  
  16. SELECT * FROM @listas
  17. WHERE 1 =
  18. CASE
  19. WHEN @Sede = 'TD' THEN CASE WHEN Origen IN ('AF','CP','LL','EC','PE') THEN 1 ELSE 0 END
  20. WHEN @Sede = 'SS' THEN CASE WHEN Origen IN ('LL','EC','PE') THEN 1 ELSE 0 END
  21. ELSE CASE WHEN Origen = @Sede THEN 1 ELSE 0 END
  22. END
  23.  
  24. SET NOCOUNT OFF

Report this snippet  

You need to login to post a comment.