{"id":5729,"date":"2013-06-15T00:00:07","date_gmt":"2013-06-15T00:00:07","guid":{"rendered":"http:\/\/craftydba.com\/?p=5729"},"modified":"2016-04-22T13:32:13","modified_gmt":"2016-04-22T13:32:13","slug":"logic-function-choose","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5729","title":{"rendered":"Logical Functions &#8211; CHOOSE()"},"content":{"rendered":"<p>I am going write a couple of tidbits on Transaction SQL <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh213226.aspx\">logical functions<\/a>.  <\/p>\n<p>These functions were introduced with the SQL Server 2012 release.  Both functions have their origins from Visual Basic for Applications (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Visual_Basic_for_Applications\">VBA<\/a>) framework. <\/p>\n<p>I think of these function as decision trees.  Especially when they are nested to several levels.  <\/p>\n<p>If you have a kid like I do, you might find the decision tree for dropped food funny.  Please see image to left.<\/p>\n<p>I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh213019.aspx\">CHOOSE<\/a> function today. <\/p>\n<p>If you grew up like I did, you probably saw black and white re-runs of the <a href=\"http:\/\/en.wikipedia.org\/wiki\/The_Three_Stooges\">Three Stooges<\/a> on television.<\/p>\n<p>The example below uses the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms177610.aspx\">RAND<\/a> function, to get a random index value.  The index value is used with the CHOOSE() function to return the culprit who broke our chair.  <\/p>\n<p>Many chairs were broken in the Three Stooges slapstick skits.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"choose() function\">\r\n\r\n--\r\n--  Choose Function\r\n--\r\n\r\n-- Who broke the chair?\r\ndeclare @idx int = CAST((RAND() * 3) AS INT) + 1;\r\nselect @idx as idx, CHOOSE(@idx, 'Moe', 'Larry', 'Curley') as Culprit\r\ngo 5\r\n\r\n<\/pre>\n<\/p>\n<p>The output of each of the five batches is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nBeginning execution loop\r\n\r\nidx         Culprit\r\n----------- -------\r\n1           Moe\r\n\r\nidx         Culprit\r\n----------- -------\r\n2           Larry\r\n\r\nidx         Culprit\r\n----------- -------\r\n3           Curley\r\n\r\nidx         Culprit\r\n----------- -------\r\n2           Larry\r\n\r\nidx         Culprit\r\n----------- -------\r\n3           Curley\r\n\r\n\r\nBatch execution completed 5 times.\r\n<\/pre>\n<\/p>\n<p>The function takes an index value N and a list of items.  It returns the Nth item in the list.<\/p>\n<p>Please note the number five after the key word <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188037.aspx\">GO<\/a>.  This tells the transaction processor to run the commands (batch) five times.<\/p>\n<p>Next time, I will be talking about the immediate if, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh213574.aspx\">IIF<\/a> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going write a couple of tidbits on Transaction SQL logical functions. These functions were introduced with the SQL Server 2012 release. Both functions have their origins from Visual Basic for Applications (VBA) framework. I think of these function as decision trees. Especially when they are nested to several levels. If you have a kid like I do, you might find the decision tree for dropped food funny. Please see image to left. I will be exploring the CHOOSE function today. If you grew up like I did, you&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[859,31,861,15,858,860,28,29],"class_list":["post-5729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-choose","tag-database-developer","tag-go","tag-john-f-miner-iii","tag-logic-functions","tag-rand","tag-sql-server","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5729"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5729\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5742"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}