Skip to content Skip to sidebar Skip to footer

Use Case In Where Clause

I am trying to add case statement to where condition in a query. My query looks like below- select * from table_name where if(id==1) then col1 = 0 else col1 is null As s

Solution 1:

SELECT*FROM table_name
WHERE isnull(id, '')=CASE(id)
               WHEN1THEN0ELSE''END;

Solution 2:

I am not sure of your requirement but you can try if below satisfies you

select * from table_name
whereisnull(col1,'') = casewhen id = 1 then   0else'' end

Also try

select * from table_name
where 
( col1 = 0and id = 1) OR ( col1 isnulland id<>1)

Solution 3:

I think you are looking for a case when solution. Which can be used as below to fill in a specific column. You do need to select the other columns as well when you do this though.

selectcasewhen id = 1 then 0elsenull end as col1
from table_name

Solution 4:

I think this is what you're looking for:

selectcasewhen id = 1 then 0elsenull end as col1, * from table_name

Post a Comment for "Use Case In Where Clause"