Intellipaat 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
3 answers
asked Dec 16, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
3 answers
0 votes
2 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...