From the course: Querying Microsoft SQL Server 2022

Unlock the full course today

Join today to access over 24,200 courses taught by industry experts.

The IIF logical function

The IIF logical function

- [Instructor] The logical function IIF will evaluate a condition and return one value if the condition is true and a different value if the condition is false. Using this function, you can have SQL Server make decisions about your data for you or help you place records into groups. To see how this works, I want to take a look at the year-to-date sales history of the employees at AdventureWorks. We'll select the BusinessEntityID and SalesYTD columns from the Sales.SalesPerson table. This shows me each employee's ID number and their current sales year-to-date. Let's suppose that we have a sales goal of $2 million, and I'd like to find out which employees have met that target goal and which ones have not. You could sort the rows or create a WHERE clause that pulls out just the people with a value over 2 million. But instead, I'd like to create a new column that reveals this information right here in these results. To…

Contents