CASE IN WHERE SENTENCE


/ Published in: SQL
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.