January 14, 2013 at 8:11 am
Koen Verbeeck (1/14/2013)
Great question about a new 2012 feature. Thanks Ron!
I must concur... that's a neat function that I had missed. I got this one right because there was only one answer that made sense, returning an integer after being given a bunch of comma separated strings wouldn't make sense to me, but I didn't know about choose until now. 🙂
January 14, 2013 at 8:33 am
Thanks, for the reminder about the Choose function. The beauty is the ability to provide two things, an alternate name or identification as well as a different sort order than if the value provided was the content of the field. Of course this really only works well if there is a short list of values to translate.
January 14, 2013 at 8:36 am
Toreador (1/14/2013)
What would be a good use of this function?Why wouldn't you just add a lookup table containing the index and description, and join to that?
So that any changes required to a description could be made by just updating the lookup table, rather than rewriting SQL.
This was exactly what I was wondering.
January 14, 2013 at 9:09 am
SQLRNNR (1/12/2013)
Glad we have this feature added.
But are the CHOOSE function and its also-new-to-SQL-Server-2012 buddy IIF anything other than syntactical sugar for Visual Basic coders writing T-SQL? The CHOOSE function is just a less-flexible shorthand for the ANSI-standard CASE, and it can only be used when evaluating an equality condition for an integer value (the index argument), although I concede that CHOOSE requires less typing than CASE in these limited circumstances. Similarly, the IIF function saves a few keystrokes when there are only two possible values to return, but once you start nesting IIF statements, the code becomes more difficult to understand than the equivalent CASE expression, in my opinion.
SQL Server 2012 just converts CHOOSE and IIF function calls to CASE expressions anyway. I have a code sample that shows that the CHOOSE and IIF functions produce Compute Scalar operators that have Defined Values that are identical to the one produced by the equivalent CASE expression, but I'm having some trouble posting it. I'll try again later.
Edit: I have posted a screenshot of the code, a .txt file of the code, and a .sqlplan file of the execution plan that I get on my SQL Server 2012 instance, if you're interested. Still not sure why I can't post code - I get an error screen that suggests that my employer is blocking the outgoing traffic when it includes certain kinds of code snippets - hmmm . . .
Jason Wolfkill
January 14, 2013 at 9:26 am
L' Eomot Inversé (1/12/2013)
Nice clear straightforward question.Good to see a question on this new feature.
Interestingly, there's an error on the BoL page: it says the CHOOSE function
BoL
Returns the data type with the highest precedence from the set of types passed to the functionThe first argument is an integer, which has a higher type precedence than any character type, but if it really was going to return an int we would have string to int conversion errors here. BoL should instead say that the return type is highest precedence type of the arguments other than the first.
I read this and changed my mind to integer.
Ah well, another point gone begging.
January 14, 2013 at 9:28 am
L' Eomot Inversé (1/12/2013)
Nice clear straightforward question.Good to see a question on this new feature.
Interestingly, there's an error on the BoL page: it says the CHOOSE function
BoL
Returns the data type with the highest precedence from the set of types passed to the functionThe first argument is an integer, which has a higher type precedence than any character type, but if it really was going to return an int we would have string to int conversion errors here. BoL should instead say that the return type is highest precedence type of the arguments other than the first.
I read this and changed my mind to integer.
Ah well, another point gone begging.
January 14, 2013 at 11:59 am
Koen Verbeeck (1/14/2013)
Great question about a new 2012 feature. Thanks Ron!(and thanks for including me ;-))
+1
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 14, 2013 at 12:53 pm
Fun, easy, and educational. Excellent!
Not all gray hairs are Dinosaurs!
January 14, 2013 at 5:16 pm
Thanks for the 2012 question. I'm not sure why I would ever use CHOOSE. I would usually join to a reference table, or use a case statement, or create a temp table if it was a small set of values to type up for an ad-hoc query or one off report. This does seem more geared towards .NET developers. I think if I came across it in production I would have to turn the array into a reference table.
January 15, 2013 at 1:00 pm
KWymore (1/14/2013)
Thanks for the 2012 question. I'm not sure why I would ever use CHOOSE. I would usually join to a reference table, or use a case statement, or create a temp table if it was a small set of values to type up for an ad-hoc query or one off report. This does seem more geared towards .NET developers. I think if I came across it in production I would have to turn the array into a reference table.
If you would sometimesd use a CASE expression for this, why object to CHOOSE? It's just a simplified syntax for CASE in a particular case. Presumably it does index into the list, so it will have better performance than case when it is applicable (because the optimiser surely isn't going to look and see if the set of values for a simple case statement provides an ungapped sequence).
Tom
January 15, 2013 at 1:12 pm
L' Eomot Inversé (1/15/2013)
KWymore (1/14/2013)
Thanks for the 2012 question. I'm not sure why I would ever use CHOOSE. I would usually join to a reference table, or use a case statement, or create a temp table if it was a small set of values to type up for an ad-hoc query or one off report. This does seem more geared towards .NET developers. I think if I came across it in production I would have to turn the array into a reference table.If you would sometimesd use a CASE expression for this, why object to CHOOSE? It's just a simplified syntax for CASE in a particular case. Presumably it does index into the list, so it will have better performance than case when it is applicable (because the optimiser surely isn't going to look and see if the set of values for a simple case statement provides an ungapped sequence).
Hi Tom,
Check my post above (about a page back in this thread) - the optimizer produces an execution plan with a Compute Scalar operator for CHOOSE with a defined value that is identical to that of CASE. It looks like the T-SQL implementation of CHOOSE truly is nothing but an alternative syntax for CASE when writing a conditional expression based on an equality comparison of integer values. SQL Server treats them exactly the same way.
Jason
Jason
Jason Wolfkill
January 16, 2013 at 1:28 pm
wolfkillj (1/15/2013)
L' Eomot Inversé (1/15/2013)
KWymore (1/14/2013)
Thanks for the 2012 question. I'm not sure why I would ever use CHOOSE. I would usually join to a reference table, or use a case statement, or create a temp table if it was a small set of values to type up for an ad-hoc query or one off report. This does seem more geared towards .NET developers. I think if I came across it in production I would have to turn the array into a reference table.If you would sometimesd use a CASE expression for this, why object to CHOOSE? It's just a simplified syntax for CASE in a particular case. Presumably it does index into the list, so it will have better performance than case when it is applicable (because the optimiser surely isn't going to look and see if the set of values for a simple case statement provides an ungapped sequence).
Hi Tom,
Check my post above (about a page back in this thread) - the optimizer produces an execution plan with a Compute Scalar operator for CHOOSE with a defined value that is identical to that of CASE. It looks like the T-SQL implementation of CHOOSE truly is nothing but an alternative syntax for CASE when writing a conditional expression based on an equality comparison of integer values. SQL Server treats them exactly the same way.
Jason
Jason
Sometimes is better to be late to the discussion: learned lot from Tom's comments.
So thanks to both Ron and Tom!
January 17, 2013 at 12:06 am
Tom, thanks for your comments about Return type.
I don't have 2012 to understand it further.
--
Dineshbabu
Desire to learn new things..
January 22, 2013 at 2:29 am
wow... nice question.. really i learned new choose() functionality. i never ever used it.:w00t:
Manik
You cannot get to the top by sitting on your bottom.
February 22, 2013 at 1:03 am
learned something about something I will never use 🙂
Viewing 15 posts - 16 through 30 (of 31 total)
You must be logged in to reply to this topic. Login to reply