Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k 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 in SQL by Appu (6.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...