SQL: IF clause within WHERE clause

635    Asked by ChloeBurgess in Tableau , Asked on Jul 12, 2021

 Is it possible to use an IF clause within a WHERE clause in MS SQL?

Example:

WHERE
    IF IsNumeric(@OrderNumber) = 1
        OrderNumber = @OrderNumber
    ELSE
        OrderNumber LIKE '%' + @OrderNumber + '%'

Answered by Carol Bower

To use an IF clause within a WHERE clause, you can try using the CASE statement like this:

WHERE OrderNumber LIKE

  CASE WHEN IsNumeric(@OrderNumber) = 1 THEN 
    @OrderNumber
  ELSE
    '%' + @OrderNumber
  END

Your Answer

Interviews

Parent Categories