Suppose I have a variable @hasAge
which can have value either 1
or 0
.
I want to write a SELECT query with where clause in which I want to check certain condition if @var
is 1 otherwise I don't want to check that condition.
Something like this:
SELECT * FROM My_Table mt
WHERE mt.name = 'abcd'
AND
IF(@hasAge)
mt.age > 10
So I want to compare my.age > 10 only if @hasAge
is 1
I dont want to have 2 different select statement like this:
if(@hasAge)
BEGIN
SELECT * FROM My_Table mt
WHERE mt.name = 'abcd'
AND
mt.age > 10
ELSE
BEGIN
SELECT * FROM My_Table mt
WHERE mt.name = 'abcd'
END