Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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 + '%'

1 Answer

0 votes
by (40.7k points)

You can try using the CASE statement like this: 

Refer to this video to learn Case Statements in detail:

WHERE OrderNumber LIKE

  CASE WHEN IsNumeric(@OrderNumber) = 1 THEN 

    @OrderNumber 

  ELSE

    '%' + @OrderNumber

  END

 

Related questions

0 votes
1 answer
+2 votes
1 answer
0 votes
1 answer
asked Jan 5, 2021 in SQL by Appu (6.1k points)
0 votes
1 answer

Browse Categories

...