So in my blog post today I’m going to talk about the CHOOSE function inside of Microsoft SQL Server. This function was introduced with the release of SQL 2012 and while I don’t use it all that often I thought it would be nice to bring it up. The image below is exactly what comes to my mind when I think of this function.
So what is this CHOOSE I speak of? It’s simply allows you to specify a set of values in a query and then select the option you want back. You can get the complete breakdown of this function from Microsoft by clicking here.
The Code:
So using the AdventureWorks2016 database I am going to write out a simple CHOOSE function against the Production.Product table. To help further drive the concept I provided 3 examples of how it works.
The Point:
As you can see it’s a fairly simple way to provide some logic to a query. I know in my day to day I’m often asked to display the same data different ways while management decides on how they want it. With this I only code it once and then specify my results at the end. The Microsoft link I shared at the intro has some great examples.