CHOOSE Function – SQL Server Denali

Written by VidhyaSagar. Posted in General

SQL Server Denali comes with one more new logical function which helps to pull data at the specified index from a list of values. CHOOSE() is the function that’s been added. Again this function exists in Access , so if you migrate the db from Access there is no need to rewrite your query using CASE statement. In simple this function is a short form of CASE statement.

Syntax for this function is below

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

Let’s populate some data to test this function

USE Tempdb
GO
CREATE TABLE tblchoose(product_id INT, stock bit)
INSERT tblchoose VALUES(1,0),(2,1),(3,1),(4,0),(5,1)

I’m going to choose on product_id column so that based on the input value it will return the product name

SELECT
CHOOSE(product_id,'Driller','Hammer','Axe','Shower Cap','Steel Plate')
AS 'Product Name'
,IIF(stock=1,'In Stock', 'Out of Stock') AS Stock
FROM tempdb..tblchoose

choose_function_1

From the query above you can see that it’s returning the data from the specified index , in this case the index is the product name. For 1 it’s returning the value Driller for 3 it’s returning Axe like that based on the input value. If you have more values in index then your choose query will be looking big it’s better if you have lesser index value.

VN:F [1.9.17_1161]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)
CHOOSE Function - SQL Server Denali, 5.0 out of 5 based on 1 rating

Tags: , , , ,

Trackback from your site.

Comments (1)

  • DotNetShoutout

    |

    SQL-Articles » CHOOSE Function – SQL Server Denali…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    Reply

Leave a comment

*

Recent Comments

Amit Bhatt

|

Hi Deepak,

Thanks for such a nice article.

You missed one thing to add in code:
@article = ‘all’,

Hence the script will be like this:
EXEC sp_addsubscription
@publication = ‘mypublication’,
@article = ‘ALL’,
@subscriber = ‘Subscriberservername’,
@destination_db = ‘mydestinationdbname’,
@reserved=’Internal’

Error 18486 | Platformblog

|

[...] SQL-Articles » Troubleshooting Login failed Error 18456This is one of the infamous error message (and number) that most of the DBAs …. 18486. Login failed for user ‘%.*ls’ because the account is currently locked out. [...]

VidhyaSagar

|

Naveen,
I’ll check this out and get back to you.

balakiran

|

Thanks man, Very simple & easy to understand !!!!