I'm trying to add a column in where clause in SQL Server 2005, but issue is I have to add it for a given condition like
where condition 1
and condition 2
if(@val !=null and @val = 'test')
begin
and condition 3
end
but when I try this SQL Server gives me error:
Incorrect syntax near the keyword 'and'. Severity 15 Procedure Name
Incorrect syntax near 'END'. Severity 15 Procedure Name
I did some R&D and found that I should use case statement instead and then I tried this
where condition 1
and condition 2
CASE @val
WHEN 'test' THEN AND condition 3
END
but now it's giving error:
Incorrect syntax near the keyword 'CASE'. Severity 15 Procedure Name
Can any one help me to sort out the problem? Thanks.
EDIT
select *
from Tables with joins
where ac_jd_date_closed >= convert(VARCHAR(10),dateadd(dd, -@period, getdate()), 101)
and ac_jd_date_closed <= convert(VARCHAR(10),dateadd(dd, 0, getdate()), 101)
and lo_pc_last_name not in ('Shroder')
and lm_ap_pr_id NOT IN (4743, 2683)
I want to add this condition when spm David otherwise this condition should not be there.