CASE statement in WHERE clause of SQL Query
I want to write a query that builds a WHERE clause at runtime based on a condition. This is what I want to do.
SELECT name from data_table WHERE
CASE
WHEN input_data = location THEN <where condition should be based on location>
WHEN input_data = college THEN <where condition should be based on college name>
How should I do it? Is it possible?
+3
Ritwik Dey
source
to share
2 answers
Try the following:
SELECT name
from data_table
WHERE
CASE
WHEN input_data = location and name like 'B%' THEN 1
WHEN input_data = college and name like 'C%' THEN 1
END = 1
Add your conditions instead of conditions name like 'B%'
and name like 'C%'
.
0
zaratustra
source
to share
SELECT name from data_table
WHERE (input_data = location AND condition based on location)
OR (input_data = college AND condition based on college name)
+1
jarlh
source
to share