Ryan
Ryan

Reputation: 115

IF on WHERE Clause

Hi how can I make this query work. I want a condition on where clause, that if @BACHNUMB = '', then WHERE is (h.sopnumbe = @SOPNUMBE) Else WHERE is (h.bachnumb = @BACHNUMB). Thanks in advance.

WHERE
CASE(@BACHNUMB)
WHEN '' THEN (h.sopnumbe = @SOPNUMBE)
ELSE
(h.bachnumb = @BACHNUMB)
END

Upvotes: 0

Views: 82

Answers (2)

yukaizhao
yukaizhao

Reputation: 690

(@BACHNUMB = '' and h.sopnumbe = @SOPNUMBE) or (@BACHNUMB != ' and 'h.bachnumb = @BACHNUMB)

Upvotes: 2

Chris Hayes
Chris Hayes

Reputation: 12020

Simply recreate the logic through different syntax:

WHERE
(@BACHNUMB = '' AND h.sopnumbe = @SOPNUMBE) 
OR
(@BACHNUMB != '' AND h.bachnumb = @BACHNUMB)
END

Upvotes: 4

Related Questions