loading..
Ðóññêèé    English
17:05

CASE statement page 4

Beginning from version 2012, IIF function is available in  SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Server. This function is well known to those who use Access  Visual Basic for ApplicationsVBA and is an alternative to the CASE expression in standard SQL. The syntax of IIF function is

  1. IIF(< condition>, < expression IF condition IS true>, < expression IF condition IS NOT true>)

    The function returns the value of expression in the second parameter, if the condition is evaluated as true, or the value of expression in the third parameter otherwise. So, the function

  1. IIF(condition, expression_1, expression_2)
is equivalent to the following CASE statement:

  1.    CASE WHEN condition THEN expression_1 ELSE expression_2 END

Using IIF function, we can rewrite the solution of the first task as follows:

Console
Execute
  1. SELECT DISTINCT product.model,
  2.     IIF(price IS NULL'Not available', CAST(price AS CHAR(20))) price
  3.     FROM Product LEFT JOIN
  4.     PC ON Product.model = PC.model
  5.     WHERE product.type = 'PC';

    In the case where there are more than two variants of branching, you can use nested IIF functions. For example, solution for task 5.10.1 you can get with the following query:  

Console
Execute
  1. SELECT DISTINCT model, price,
  2.     IIF(price=(SELECT MAX(price) FROM PC), 'Most expensive',
  3.         IIF(price=(SELECT MIN(price) FROM PC), 'Most cheap', 'Mean price')) comment
  4.     FROM PC
  5.     ORDER BY price;

    If situation will go in the same manner, SWITCH statement could soon appear in T-SQL. :-)

Bookmark and Share
Pages 1 2 3 4
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
https://exchangesumo.com/obmen/XRP-PRRUB/
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.