160k views
1 vote
Select case ( when..... then.... when.. then.. else end as...) from...?

User Baotiao
by
7.4k points

1 Answer

3 votes

Final answer:

Yes, there is a shorthand syntax in MS SQL Server that can be used as a shortcut for the CASE WHEN statement. Instead of writing "CASE WHEN bit THEN 'something' ELSE null END", you can use the IIF function.

Step-by-step explanation:

The IIF function is a logical function that takes a Boolean expression and returns one of two values based on the result of the expression.

The syntax for using the IIF function is as follows:

```

SELECT IIF(bit, 'something', null) AS columnName

FROM tableName

```

In this syntax:

  • - `bit` is the Boolean expression or condition that you want to evaluate.
  • - `'something'` is the value to return if the condition is true.
  • - `null` is the value to return if the condition is false.
  • - `columnName` is the name you want to give to the column in the result set.
  • - `tableName` is the name of the table from which you are selecting the data.

Here's an example to illustrate how the IIF function can be used:

```

SELECT IIF(Sales > 1000, 'High', 'Low') AS SalesStatus

FROM Orders

```

In this example, if the sales value is greater than 1000, the result will be 'High', otherwise, it will be 'Low'.

Using the IIF function can make your SQL code more concise and readable, especially when dealing with simple conditional expressions. However, please note that the IIF function can only evaluate one condition and provide two results. If you have multiple conditions or need more flexibility, the CASE WHEN statement may be more suitable.

Your question is incomplete, but most probably the full question was:

Is there a shortcut in MS SQL Server for a call CASE WHEN bit THEN 'something' ELSE null END ? Is there a way to write it differently?

select case ( when..... then.... when.. then.. else end as...) from...?

User Szydan
by
7.6k points