Skip to content

Latest commit

 

History

History
126 lines (95 loc) · 4.14 KB

File metadata and controls

126 lines (95 loc) · 4.14 KB
title CHOOSE (Transact-SQL) | Microsoft Docs
ms.custom
ms.date 03/14/2017
ms.prod sql-non-specified
ms.reviewer
ms.suite
ms.technology
database-engine
ms.tgt_pltfrm
ms.topic language-reference
f1_keywords
CHOOSE
CHOOSE_TSQL
dev_langs
TSQL
helpviewer_keywords
CHOOSE function
ms.assetid 1c382c83-7500-4bae-bbdc-c1dbebd3d83f
caps.latest.revision 13
author BYHAM
ms.author rickbyh
manager jhubbard

Logical Functions - CHOOSE (Transact-SQL)

[!INCLUDEtsql-appliesto-ss2012-asdb-xxxx-xxx_md]

Returns the item at the specified index from a list of values in [!INCLUDEssNoVersion].

Topic link icon Transact-SQL Syntax Conventions

Syntax

  
CHOOSE ( index, val_1, val_2 [, val_n ] )  

Arguments

index
Is an integer expression that represents a 1-based index into the list of the items following it.

If the provided index value has a numeric data type other than int, then the value is implicitly converted to an integer. If the index value exceeds the bounds of the array of values, then CHOOSE returns null.

val_1 … val_n
List of comma separated values of any data type.

Return Types

Returns the data type with the highest precedence from the set of types passed to the function. For more information, see Data Type Precedence (Transact-SQL).

Remarks

CHOOSE acts like an index into an array, where the array is composed of the arguments that follow the index argument. The index argument determines which of the following values will be returned.

Examples

The following example returns the third item from the list of values that is provided.

SELECT CHOOSE ( 3, 'Manager', 'Director', 'Developer', 'Tester' ) AS Result;  

[!INCLUDEssResult]

Result  
-------------  
Developer  
  
(1 row(s) affected)  

The following example returns a simple character string based on the value in the ProductCategoryID column.

USE AdventureWorks2012;  
GO  
SELECT ProductCategoryID, CHOOSE (ProductCategoryID, 'A','B','C','D','E') AS Expression1  
FROM Production.ProductCategory;  
  

[!INCLUDEssResult]

ProductCategoryID Expression1  
----------------- -----------  
3                 C  
1                 A  
2                 B  
4                 D  
  
(4 row(s) affected)  
  

The following example returns the quarter in which an employee was hired. The MONTH function is used to return the month value from the column HireDate.

USE AdventureWorks2012;  
GO  
SELECT JobTitle, HireDate, CHOOSE(MONTH(HireDate),'Winter','Winter', 'Spring','Spring','Spring','Summer','Summer',   
                                                  'Summer','Autumn','Autumn','Autumn','Winter') AS Quarter_Hired  
FROM HumanResources.Employee  
WHERE  YEAR(HireDate) > 2005  
ORDER BY YEAR(HireDate);  
  

[!INCLUDEssResult]

JobTitle                                           HireDate   Quarter_Hired  
-------------------------------------------------- ---------- -------------  
Sales Representative                               2006-11-01 Autumn  
European Sales Manager                             2006-05-18 Spring  
Sales Representative                               2006-07-01 Summer  
Sales Representative                               2006-07-01 Summer  
Sales Representative                               2007-07-01 Summer  
Pacific Sales Manager                              2007-04-15 Spring  
Sales Representative                               2007-07-01 Summer  
  

See Also

IIF (Transact-SQL)