StefanG
StefanG

Reputation: 269

Check a certain field for not null based on a parameter in sql

I get data from a stored procedure. Based on a passed Parameter I want to Change the SQL Statement in the where clause

The Statement Looks like this

    SELECT 
      CC.Id,
      CC.TId,
      CC.PId,
      CC.Date
    FROM Mytable CC
    WHERE CC.TId IS NOT NULL

I pass a Parameter @Qualifier to the procedure so I want to check this param. If it is '1' this SQL Statement should be executed, otherwise the second SQL Statement should be executed:

    SELECT 
      CC.Id,
      CC.TId,
      CC.PId,
      CC.Date
    FROM Mytable CC
    WHERE CC.PId IS NOT NULL

I tried to achieve this using the where clause like this

    SELECT 
      CC.Id,
      CC.TId,
      CC.PId,
      CC.Date
    FROM Mytable CC
    WHERE 
     (CASE
WHEN @Qualifier = '1'
  THEN CC.TId IS NOT NULL
ELSE CC.PId IS NOT NULL)

But that is not working. Anyone knows how to solve this?

Upvotes: 0

Views: 34

Answers (1)

jarlh
jarlh

Reputation: 44696

You mean:

WHERE (@Qualifier = '1' and CC.TId IS NOT NULL)
   OR (@Qualifier <> '1' and CC.PId IS NOT NULL)

Upvotes: 1

Related Questions