CHOOSE() in SQL server 2012

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 638
CHOOSE() function introduces in SQL Server 2012. first argument represents the Index value to choose value from a list of values.

This one returns the data type with highest precedence from the set of types passed to the function.

SELECT CHOOSE(2, 100, 200, 300, 400, 500 ) AS [Output]  -- 200 ( return type is INT)

The below CHOOSE returns datetime type data....

SELECT CHOOSE(1, 1, 'Option 1', GETDATE(), 3.14159, PI() ) AS [Output] -- 1900-01-04 03:23:53.603
SELECT CHOOSE(3, 1, 'Option 1', GETDATE(), 3.14159, PI() ) AS [Output] --2014-09-19 10:00:50.570
SELECT CHOOSE(4, 1, 'Option 1', GETDATE(), 3.14159, PI() ) AS [Output] -- 1900-01-04 03:23:53.373
SELECT CHOOSE(5, 1, 'Option 1', GETDATE(), 3.14159, PI() ) AS [Output] -- 1900-01-04 03:23:53.603
SELECT CHOOSE(2, 1, 'Option 1', GETDATE(), 3.14159, PI() ) AS [Output] -- raises ERROR due to string datetime conversion failed
/*
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.*/

Comments or Responses

Login to post response