Home > SQL Server > SQL SERVER 2012 – CHOOSE() a Logical Function @SQLSERVER

SQL SERVER 2012 – CHOOSE() a Logical Function @SQLSERVER


CHOOSE() : It is a new logical function introduced in SQL Server 2012 version.  This function holds a list of values and returns a value based on the location index number.

Usage

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

Example

1. Printing Boolean name.

Executing the following query

SELECT CHOOSE(1, ‘TRUE’, ‘FALSE’) AS ‘BOOLEAN’

pic1The Index location value of this function should be an integer value and it starts from 1 to N whole number.

If we use the Index value out of boundary of the list then it returns a NULL value.

SELECT CHOOSE(0, ‘TRUE’, ‘FALSE’) AS ‘Index 0’
SELECT CHOOSE(3, ‘TRUE’, ‘FALSE’) AS ‘Index 3’

These SQL queries will return NULL value. Because, the index location is out of boundary.

pic2This logical function is very helpful for the developers to handle an array or list of value in the T-SQL code. It reduces the line of code and simplifies the logic.

Learn all new functions that are introduced in SQL Server 2012 in “New Functions in SQL Server 2012” blog post.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: