Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - any equivalent of Excel's CHOOSE function?

I'd be surprised if this hasn't been asked before, but I haven't been able to find anything. Excel has a function

CHOOSE(n, x_1, x_2, x_3, ...)

which returns x_n for the given value of n.

Is there anything similar in SQL (either standard or MS-specific) supported by SQL Server 2008? I know it should really be implemented using a lookup table in the database, but for what I'm doing I'm not able to add new tables to the database.

I could create a temporary table and populate it from the SQL script, or use

CASE n WHEN 1 THEN x_1 WHEN 2 THEN x_2 WHEN 3 THEN x_3 ... END

but is there anything less cumbersome?

like image 826
Stewart Avatar asked Mar 16 '23 07:03

Stewart


1 Answers

Unfortuantely, no it seems not to be the present in your version.

The CHOOSE-Function is only available since SQL Server 2012 and works quite the same as you describe the Excel-function.

like image 62
DrCopyPaste Avatar answered Mar 18 '23 21:03

DrCopyPaste