Question: I am trying to filter a SELECT statament based on whether a variable is a number or not. The behavior I want is that if the parameter @productId is not a number then I want the WHERE clause to do the following:

```
WHERE (DTH.TaskId IS NULL OR PDT.PK_Product LIKE @productId)

```

If @productId is a number I want the following WHERE clause:

```
WHERE (PDT.PK_Product = @productId)

```

I thought the following would work:

```
    WHERE (ISNUMERIC(@productId) = 0 AND DTH.TaskId IS NULL
            OR PDT.PK_Product LIKE @productId)
    AND (ISNUMERIC(@productId) = 1 AND PDT.PK_Product = @productId)

```

because if ISNUMERIC(@productId) evaluates to 0 it would not use the second part of the statement and if 1 the first. 

Edit:

The problem I have is that if productId is numeric (and ISNUMERIC(@productId) evaluates to 1) I am still getting results where DTH.TaskId IS NULL.

Answer: 
You need extra parentheses around the `OR` conditional, since `AND` takes precedence. You can't use `AND` between the conditional groups, because `@productId` can only be numeric or not, not both. You also need to filter out the `NOT NULL` values from the second conditional. So:

```
WHERE 
(
  ISNUMERIC(@productId) = 0 AND 
  (
    DTH.TaskId IS NULL
    OR PDT.PK_Product LIKE @productId
  ) -- <-- these parens are important
)
OR -- <-- this can't be AND - @productId can't be numeric and not numeric
(
  ISNUMERIC(@productId) = 1 
  AND DTH.TaskID IS NOT NULL -- <-- this clause is important according to edit
  AND PDT.PK_Product = @productId
);

```
you are close, this should do the trick

```
WHERE (ISNUMERIC(@productId) = 1 AND PDT.PK_Product = @productId AND DTH.TaskId IS NOT NULL) OR
      (ISNUMERIC(@productId) = 0 AND (DTH.TaskId IS NULL OR PDT.PK_Product LIKE @productId))

```