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.3k 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
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...