I searched everything and found questions about the conditional where clauses, but reported nothing about the conditional addition of the fragment andto where clause. I am not a sql / database user, I'm from another area ITthat was thrown into the “try to fix it” problem today because our regular database users have disappeared. I have basic concepts and knowledge in the field of programming, but sqlfor me it's just a strange animal.
I have a simple one:
SELECT abc FROM table WHERE sdate=@somedt
But I need to add:
AND otherdt=@otherdt
but only if @otherdtnot null. If it is zero, I don’t want to add a part at all and. I tried several ways to do this, but it keeps giving me an error. Any help is appreciated. This was my last unsuccessful attempt:
SELECT abc FROM table
WHERE sdate=@somedt
IF @otherdt IS NOT NULL
BEGIN
AND otherdt=@otherdt
END
source
share