user1428798 user1428798 - 1 month ago 9
SQL Question

conditional where Sql server

I have a query

select idpays,idannonce,idville
from annonces


and i need to make a conditional where, where
idpays=1
don't show
idville
null
else for other
idpays
show
idville
null


How can i do this please?

regards

Answer

You don't specify what, exactly, should be displayed when idpays=1, but here's a basis for what you've asked - you can do this with a case in the SELECT which sounds more like the problem you've described rather than a where clause:

select idpays,idannounce, 
       case when idpays=1 then idville 
       else null end
  from unknowntable

EDIT based on OP comments- must admit I'm not sure I understand the OP's requirements exactly at this point:

select 
  from table
 where (idpays<>1) or
       (idpays=1 and idville not null)