{"id":1854,"date":"2014-08-13T00:00:00","date_gmt":"2014-08-13T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/questions-about-t-sql-expressions-you-were-too-shy-to-ask\/"},"modified":"2026-05-14T08:30:19","modified_gmt":"2026-05-14T08:30:19","slug":"questions-about-t-sql-expressions-you-were-too-shy-to-ask","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/t-sql-programming-sql-server\/questions-about-t-sql-expressions-you-were-too-shy-to-ask\/","title":{"rendered":"Questions about T-SQL Expressions You Were Too Shy to Ask"},"content":{"rendered":"\n<div id=\"pretty\">\n<h4>Expressions in T-SQL. Have any questions you&#8217;ve been too shy to ask? Robert Sheldon is here to help&#8230;<\/h4>\n<p><em>Part of the &#8216;Too Shy to Ask&#8217; series on Simple Talk &#8211; <a href=\"https:\/\/www.red-gate.com\/simple-talk\/collections\/the-complete-too-shy-to-ask-series\/\" target=\"_blank\" rel=\"noopener\">click here for more<\/a>.<\/em><\/p>\n<h2>The Questions<\/h2>\n<ol>\n<li><a href=\"#first\">What exactly is a T-SQL expression<\/a><\/li>\n<li><a href=\"#second\">I want to display outputted numbers as currency, with commas and dollar signs. Is that possible in T-SQL?<\/a><\/li>\n<li><a href=\"#third\">I see the plus sign (+) used in a variety of ways in T-SQL script, but I can&#8217;t always make sense of how SQL Server arrives at the results it does. Is there a &#8220;right way&#8221; to use plus signs?<\/a><\/li>\n<li><a href=\"#fourth\">The NULLIF expression makes no sense. I would think you&#8217;d use it to somehow ferret out a NULL value or to test a condition for a NULL value. But it appears to merely return a NULL if two values are equal. Is that all it&#8217;s doing?<\/a><\/li>\n<li><a href=\"#fifth\">Can you use expressions in data manipulation language (DML) statements?<\/a><\/li>\n<li><a href=\"#sixth\">The CASE expression allows me to leave out the ELSE clause. Is that important to include it?<\/a><\/li>\n<li><a href=\"#seventh\">Can you shed light on the order in which the database engine processes the components in an expression?<\/a><\/li>\n<li><a href=\"#eighth\">I get confused on how to treat constants in my expressions. Is there a trick in using them?<\/a><\/li>\n<li><a href=\"#ninth\">Are there any advantages to using a COALESCE expression rather than a CASE expression when checking for the first non-NULL value in a list of values?<\/a><\/li>\n<li><a href=\"#tenth\">I&#8217;m working on a query that uses the NOT IN operator in the WHERE clause. The operator checks values in a list returned by a subquery. When the returned list includes a NULL value, the query returns an empty resultset, even though I know I should be seeing results. Any idea what might be happening?<\/a><\/li>\n<li><a href=\"#eleventh\">What the heck is a &#8216;modulo&#8217;?<\/a><\/li>\n<li><a href=\"#twelveth\">Are all WHEN and ELSE clauses in a CASE expression evaluated if the first WHEN clause evaluates to TRUE?<\/a><\/li>\n<li><a href=\"#thirteenth\">What&#8217;s the difference between the two &#8216;not equal to&#8217; operators (&lt;&gt; and !=)?<\/a><\/li>\n<li><a href=\"#fourteenth\">I&#8217;ve come across operators such as += in variable assignment SET statements. What do they mean?<\/a><\/li>\n<\/ol>\n<h3 id=\"first\">What exactly is a T-SQL expression?&#8221;<\/h3>\n<div class=\"indented\">\n<p>An expression is a block of code in a T-SQL statement that is processed as a unit in order to return a scalar (single) data value. Each expression is made up of one or more types of components, including constants, columns, variables, operators, scalar functions, and scalar subqueries. SQL Server supports the use of expressions within different types of T-SQL statements and within various parts of those statements. For example, you can define expressions in the <code>SELECT<\/code>, <code>WHERE<\/code>, <code>GROUP<\/code> <code>BY<\/code>, and <code>ORDER<\/code> <code>BY<\/code> clauses of a <code>SELECT<\/code> statement.<\/p>\n<p>The following T-SQL code contains a variety of expressions throughout the <code>SET<\/code> and <code>SELECT<\/code> statements:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @date DATETIME;\n\t\t\tSET @date = GETDATE()\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 Name + ':' + SUBSTRING(ProductNumber, 4, 4) AS ProdName,\n\t\t\t\u00a0 ReorderPoint,\n\t\t\t\u00a0 CONVERT(INT, ROUND(ReorderPoint * 1.1, 0)) AS NewPoint,\n\t\t\t\u00a0 SafetyStockLevel - ReorderPoint AS PointDiff,\n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN Color IS NULL THEN 'N\/A' \n\t\t\t\u00a0\u00a0\u00a0 ELSE Color\n\t\t\t\u00a0 END AS ProdColor,\n\t\t\t\u00a0 DATEDIFF(mm, SellStartDate, @date) AS MonthDiff\n\t\t\tFROM Production.Product\n\t\t\tWHERE ProductID BETWEEN 316 AND 320;\n\t\t\t\u00a0\n\t\t<\/pre>\n<p>The first expression shows up in the <code>SET<\/code> statement, after the equal sign. It is simply the <code>GETDATE()<\/code> function. An expression is often made up of only a single element, such as a column, constant, or in this case, function.<\/p>\n<p>Next we move to the <code>SELECT<\/code> statement&#8217;s select list. The first expression we find defines the <code>ProdName<\/code> column. The expression concatenates the <code>Name<\/code> column with the last four digits of the <code>ProductNumber<\/code> column. The expression includes two concatenation operators (<code>+<\/code>), one literal string value (<code>:<\/code>), and the <code> SUBSTRING<\/code> scalar function, which extracts the last four digits from the <code>ProductName<\/code> column.<\/p>\n<p>The next expression in the select list is the <code>ReorderPoint<\/code> column. The expression and name of the column are one and the same. This is followed by an expression that defines the <code> NewPoint<\/code> column. The expression multiplies the <code>ReorderPoint<\/code> value by <code> 1.1<\/code>, uses the <code>ROUND<\/code> function to round the result to a whole number, and then uses the <code>CONVERT()<\/code> function to convert the value to an integer.<\/p>\n<p>Next we have an expression that defines the <code>PointDiff<\/code> column. In this case, the expression merely subtracts the <code>ReorderPoint<\/code> value from the <code>SafetyStockLevel<\/code> value, using the subtract operator to calculate the difference.<\/p>\n<p>Our next expression defines the <code>ProdColor<\/code> column. Here we use a <code>CASE<\/code> expression to provide a default value should a <code>NULL<\/code> be returned.<\/p>\n<p>The final expression in the select list defines the <code>MonthDiff<\/code> column. The expression uses the <code>DATEDIFF<\/code> function to calculate the difference, in months, between the <code>SellStartDate<\/code> value and the <code>@date<\/code> variable value.<\/p>\n<p>The next expression is in the search condition of the <code>WHERE<\/code> clause. The expression compares the <code>ProductID<\/code> value to the range of values defined by the <code>BETWEEN<\/code> operator, the <code>AND<\/code> operator, and the literal values <code> 316<\/code> and <code>320<\/code>. You can use various operators to link expression elements together in this way, creating one large expression that is synthesized into a single unit that returns a scalar value. Note that the search condition can also be considered to be three expressions linked together by the <code>BETWEEN<\/code> and the <code>AND<\/code> operators: one column and two numerical constants.<\/p>\n<p>Because of the expressions specified throughout the T-SQL code, our <code>SELECT<\/code> statement returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProdName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ReorderPoint<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>NewPoint<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>PointDiff<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProdColor<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>MonthDiff<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Blade:2036<\/p>\n<\/td>\n<td valign=\"top\">\n<p>600<\/p>\n<\/td>\n<td valign=\"top\">\n<p>660<\/p>\n<\/td>\n<td valign=\"top\">\n<p>200<\/p>\n<\/td>\n<td valign=\"top\">\n<p>N\/A<\/p>\n<\/td>\n<td valign=\"top\">\n<p>145<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Crankarm:5965<\/p>\n<\/td>\n<td valign=\"top\">\n<p>375<\/p>\n<\/td>\n<td valign=\"top\">\n<p>413<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>145<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ML Crankarm:6738<\/p>\n<\/td>\n<td valign=\"top\">\n<p>375<\/p>\n<\/td>\n<td valign=\"top\">\n<p>413<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>145<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Crankarm:7457<\/p>\n<\/td>\n<td valign=\"top\">\n<p>375<\/p>\n<\/td>\n<td valign=\"top\">\n<p>413<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>145<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Chainring Bolts:2903<\/p>\n<\/td>\n<td valign=\"top\">\n<p>750<\/p>\n<\/td>\n<td valign=\"top\">\n<p>825<\/p>\n<\/td>\n<td valign=\"top\">\n<p>250<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<td valign=\"top\">\n<p>145<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Clearly, expressions play an important role in a T-SQL statement. They can be as simple the ones shown here or far more complex.<\/p>\n<p>What we&#8217;ve not covered, however, is the <code>FROM<\/code> clause. In this case, the <code>FROM<\/code> clause includes only the schema and table name. In some cases, the <code>FROM<\/code> clause might include a <i>table expression,<\/i> such as a simple table-valued variable or an operand in an <code>APPLY<\/code> clause. However, those types of expressions are usually specifically referred to as <i>table expressions<\/i>. When the term <i>expression<\/i> is used by itself, it&#8217;s normally referring to a scalar expression, such as those highlighted in the example above.<\/p>\n<p>To know when and where you can use expressions, you should refer to the statement syntax in SQL Server Books Online. Not only will this show you where you can create expressions, but also whether there are any limitations on the expressions you create.<\/p>\n<\/div>\n<h3 id=\"second\">&#8220;I want to display outputted numbers as currency, with commas and dollar signs. Is that possible in T-SQL?&#8221;<\/h3>\n<div class=\"indented\">\n<p>Yes, it&#8217;s possible, but before we go into how that is done, be aware that such operations are generally best done by the application at the presentation level, rather than at the database level. Still, there are times when you are required to convert data into its representational form. So let&#8217;s look at how to format your currency.<\/p>\n<p>Prior to SQL Server 2012, to get data into a currency format (at least for currencies with a structure similar to the US dollar), you could cast the value to the <code>MONEY<\/code> type, if necessary, and then use the <code>CONVERT<\/code> function to convert the value to a character type, as shown in the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">DECLARE \n\t\t\t\u00a0 @var1 NVARCHAR(20) = '555555555.5555',\n\t\t\t\u00a0 @var2 DECIMAL(20,4) = 555555555.5555,\n\t\t\t\u00a0 @var3 MONEY = 555555555.5555;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 '<\/pre>\n<p>Each expression in the <code>SELECT<\/code> list references one of the variables as its source data and is made up of multiple components. What provides the final format, however, is the <code>CONVERT<\/code> function. The function requires that the source data be either the <code>MONEY<\/code> or <code>SMALLMONEY<\/code> type. When you call the function, you specify a second argument along with the source value. That argument determines the format of the data. In this case, we specify <code>1<\/code> so the outputted data includes commas every three digits to the left of the decimal point. We also concatenate the value with a dollar sign to give it that final touch, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>amount1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>amount2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>amount3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>$555,555,555.56<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$555,555,555.56<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$555,555,555.56<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Overall, this isn&#8217;t too painful a process, unless you&#8217;re working with currencies that follow a different format, such as the Swiss franc, which takes apostrophes rather than commas. In such cases, you might have to add yet another function, such as <code>REPLACE<\/code>, to substitute the commas with apostrophes.<\/p>\n<p>Fortunately, since the release of SQL Server 2012, you&#8217;ve been able to instead use the <code>FORMAT<\/code> function to output your numbers to a currency format. The <code>FORMAT<\/code> function returns a value in a specified format, based on a specified culture. To demonstrate how this works, let&#8217;s first recast our preceding expressions into ones that use the <code>FORMAT<\/code> function to configure the data as US dollar amounts:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE \n\t\t\t\u00a0 @var1 NVARCHAR(20) = '555555555.5555',\n\t\t\t\u00a0 @var2 DECIMAL(20,4) = 555555555.5555,\n\t\t\t\u00a0 @var3 MONEY = 555555555.5555;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 FORMAT(CAST(@var1 AS MONEY), 'c', 'en-us') AS amount1,\n\t\t\t\u00a0 FORMAT(@var2, 'c', 'en-us') AS amount2,\n\t\t\t\u00a0 FORMAT(@var3, 'c', 'en-us') AS amount3;\n\t\t\t\u00a0\n\t\t<\/pre>\n<p>Notice that we must first cast the character data (<code>@var1<\/code>) to the <code>MONEY<\/code> data type, but we can use the <code>DECIMAL<\/code> variable (<code>@var2<\/code>) as is. The second <code>FORMAT<\/code> argument (<code>c<\/code>) specifies that we&#8217;re formatting currency, and the third argument specifies the US symbol (<code>en-us<\/code>) for the cultural context. The <code>SELECT<\/code> statement returns the same results as the previous <code>SELECT<\/code> statement, with all values returned as the <code>NVARCHAR<\/code> type.<\/p>\n<p>However, with the <code>FORMAT<\/code> function, we can specify other cultures. For example, the following example uses the German symbol (<code>de-de<\/code>) to provide the cultural context:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE \n\t\t\t\u00a0 @var1 NVARCHAR(20) = '555555555.5555',\n\t\t\t\u00a0 @var2 DECIMAL(20,4) = 555555555.5555,\n\t\t\t\u00a0 @var3 MONEY = 555555555.5555;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 FORMAT(CAST(@var1 AS MONEY), 'c', 'de-de') AS amount1,\n\t\t\t\u00a0 FORMAT(@var2, 'c', 'de-de') AS amount2,\n\t\t\t\u00a0 FORMAT(@var3, 'c', 'de-de') AS amount3;\n\t\t<\/pre>\n<p>Now our results are specific to how the euro is represented in Germany, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>amount1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>amount2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>amount3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td>\n<td valign=\"top\">\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td>\n<td valign=\"top\">\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Certainly, the <code>FORMAT<\/code> function is a handy tool if you want your results to be displayed in a specific currency. But just to reiterate, such formatting is generally handled by the calling application, with the source data retrieved in its raw state.<\/p>\n<\/div>\n<h3 id=\"third\">&#8220;I see the plus sign (+) used in a variety of ways in T-SQL script, but I can&#8217;t always make sense of how SQL Server arrives at the results it does. Is there a &#8220;right way&#8221; to use plus signs?&#8221;<\/h3>\n<div class=\"indented\">\n<p>SQL Server is a tricky devil. As you&#8217;ve discovered, plus signs have all sorts of meaning in T-SQL. You can use them to concatenate string values, add numerical values together, or indicate that a numerical value is a positive number, rather than negative. The following <code>SELECT<\/code> statement demonstrates how to both concatenate and add values:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 FirstName + ' ' + LastName AS FullName,\n\t\t\t\u00a0 FORMAT(SalesLastYear, 'c', 'en-us') AS SalesLastYear,\n\t\t\t\u00a0 FORMAT(SalesYTD, 'c', 'en-us') AS SalesYTD,\n\t\t\t\u00a0 FORMAT(SalesLastYear + SalesYTD, 'c', 'en-us') AS TwoYearSales\n\t\t\tFROM Sales.vSalesPerson\n\t\t\tWHERE TerritoryName IS NOT NULL;\n\t\t<\/pre>\n<p>For the <code>FullName<\/code> column, we&#8217;ve used the plus sign to concatenate the <code>FirstName<\/code> and <code> LastName<\/code> columns with a space in between. For the <code>TwoYearSales<\/code> column, we&#8217;ve used the plus sign to add the <code>SalesLastYear<\/code> and <code> SalesYTD<\/code> columns together. In both cases, the database engine knows when to concatenate the values or when to add them together, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesLastYear<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesYTD<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>TwoYearSales<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,750,406.48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,763,178.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,513,584.66<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,439,156.03<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,251,368.55<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,690,524.58<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,997,186.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,189,418.37<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,186,604.57<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Garrett Vargas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,620,276.90<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,453,719.47<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,073,996.36<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,849,640.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,315,185.61<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,164,826.55<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,927,059.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,352,577.13<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,279,636.31<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,073,506.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,458,535.62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,532,041.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jos\u00e9 Saraiva<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,038,234.65<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,604,540.72<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,642,775.37<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,371,635.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,573,012.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,944,648.25<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$0.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Lynn Tsoflias<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,278,548.98<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,421,810.92<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,700,359.90<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Rachel Valdez<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,307,949.79<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,827,066.71<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,135,016.50<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jae Pak<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,635,823.40<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,116,871.23<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,752,694.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Ranjit Varkey Chudukatil<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,396,539.76<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,121,616.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,518,156.08<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There&#8217;s no real mystery here. When we use the plus sign with only character data, the database engine concatenates the values. In this case, the plus sign is considered a string concatenation operator. When we use the plus sign with numerical values, the database engine adds the values together. Under these circumstances, the plus sign is considered an addition operator.<\/p>\n<p>Not surprisingly, when we mix and match the types of data, our results are less predictable. Take a look at the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 NVARCHAR(10) = 'TestVar',\n\t\t\t\u00a0 @var2 CHAR(2) = 'CX',\n\t\t\t\u00a0 @var3 SMALLINT = 300,\n\t\t\t\u00a0 @var4 DECIMAL(10,4) = 1000.0001,\n\t\t\t\u00a0 @var5 BIT = 1,\n\t\t\t\u00a0 @var6 MONEY = 999.99,\n\t\t\t\u00a0 @var7 DATETIME = '2014-07-25 00:00:00.000';\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 + @var2 AS var1var2, \n\t\t\t\u00a0 @var3 + @var6 AS var3var6,\n\t\t\t\u00a0 @var4 + @var5 AS var4var5,\n\t\t\t\u00a0 @var7 + @var3 AS var7var3,\n\t\t\t\u00a0 @var6 + @var7 AS var6var7;\n\t\t<\/pre>\n<p>As you can see, we&#8217;ve declared seven variables, each defined with a different type. We then use the plus sign to combine the variables in different ways, giving us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>TestVarCX<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1299.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1001.0001<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2015-05-21 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2017-04-19 23:45:36.000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As we would expect, when we pair <code>@var1<\/code> with <code>@var2<\/code>, both character types, we end up with a concatenated string. When we pair <code>@var3<\/code> to <code>@var6<\/code>, both numerical types, the values are added together, even though <code>@var3<\/code> and <code>@var6<\/code> are different types.<\/p>\n<p>Now we get to our third pairing: <code>@var4<\/code> <code>+<\/code> <code>@var5<\/code>. This time around, we&#8217;re adding a <code>DECIMAL<\/code> value to <code>BIT<\/code> value. However, the database engine still treats both variables as numerical values and adds them together, even though SQL Server documentation suggests that you can&#8217;t do this with the <code>BIT<\/code> type. (And really, why would you?)<\/p>\n<p>The next item in the select list, <code>@var7<\/code> <code>+<\/code> <code>@var3<\/code>, pairs a <code>DATETIME<\/code> value with a <code>SMALLINT<\/code> value. This time, we get a date that is 300 days later than the original date. When your expression includes the plus sign, the database engine defaults to the data type with the highest precedence, in this case, <code> DATETIME<\/code>. Based on the type, the engine determines whether to use the plus sign to concatenate values or add them together. If the data type with the highest precedence is a numeric data type, the engine attempts to add the values. In this case, the engine adds the <code>SMALLINT<\/code> value to the <code>DATETIME<\/code> value as the specified number of days. (A <code>DATETIME<\/code> value is actually stored as two integers.)<\/p>\n<p>Finally, we pair a <code>MONEY<\/code> date type with the <code>DATETIME<\/code> date type (<code>@var6<\/code> <code>+<\/code> <code>@var7<\/code>). Once again, <code>DATETIME<\/code> takes precedence over <code>MONEY<\/code>, so the database engine adds 999.99 days to our original date to come up with a date in 2017.<\/p>\n<p>We can verify how data type precedence works with plus signs by using the <code>SQL_VARIANT_PROPERTY<\/code> function to retrieve the data type of the expressions that pair the different variables:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 NVARCHAR(10) = 'TestVar',\n\t\t\t\u00a0 @var2 CHAR(2) = 'CX',\n\t\t\t\u00a0 @var3 SMALLINT = 300,\n\t\t\t\u00a0 @var4 DECIMAL(10,4) = 1000.0001,\n\t\t\t\u00a0 @var5 BIT = 1,\n\t\t\t\u00a0 @var6 MONEY = 999.99,\n\t\t\t\u00a0 @var7 DATETIME = '2014-07-25 00:00:00.000';\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 SQL_VARIANT_PROPERTY(@var1 + @var2, 'basetype') AS var1var2, \n\t\t\t\u00a0 SQL_VARIANT_PROPERTY(@var3 + @var6, 'basetype') AS var3var6,\n\t\t\t\u00a0 SQL_VARIANT_PROPERTY(@var4 + @var5, 'basetype') AS var4var5,\n\t\t\t\u00a0 SQL_VARIANT_PROPERTY(@var7 + @var3, 'basetype') AS var7var3,\n\t\t\t\u00a0 SQL_VARIANT_PROPERTY(@var6 + @var7, 'basetype') AS var6var7;\n\t\t<\/pre>\n<p>The <code>SELECT<\/code> statement returns the returned data type for each expression, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>money<\/p>\n<\/td>\n<td valign=\"top\">\n<p>decimal<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0In each case, the expression returns the type with the highest precedence and determines how the plus sign will be used.<\/p>\n<p>So far in our examples, the database engine has been able to add or concatenate values without a problem because the engine could implicitly convert the data type with the lowest precedence to the type with the highest. But this isn&#8217;t always the case:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 NVARCHAR(10) = 'TestVar',\n\t\t\t\u00a0 @var3 SMALLINT = 300;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 + @var3 AS var1var3;\n\t\t<\/pre>\n<p>This time around, we&#8217;re using the plus sign to pair a <code>NVARCHAR<\/code> value with a <code>SMALLINT<\/code> value. Because <code>SMALLINT<\/code> has precedence over <code>NVARCHAR<\/code>, the database engine attempts to convert the value <code>TestVar<\/code> to the <code>SMALLINT<\/code> type, which of course is not possible. Consequently, the database engine returns the following error message:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tMsg 245, Level 16, State 1, Line 5\n\t\t\tConversion failed when converting the nvarchar value 'TestVar' to data type smallint.\n\t\t<\/pre>\n<p>The only way we can pair a numerical value with a string value is to concatenate them. In order to do so, we must explicitly convert the <code>SMALLINT<\/code> value to a character data type:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 NVARCHAR(10) = 'TestVar',\n\t\t\t\u00a0 @var2 SMALLINT = 300;\n\t\t\t\u00a0\n\t\t\tSELECT @var1 + CAST(@var2 AS NVARCHAR(10));\n\t\t<\/pre>\n<p>Now our <code>SELECT<\/code> statement returns the value <code>TestVar300<\/code>.<\/p>\n<p>There&#8217;s one other use of the plus sign that you should be aware of-as a unary plus operator. SQL Server supports two types of unary operators, plus and minus, which are used to designate whether a numeric value is positive or negative:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 DECIMAL(10,2) = +123.45,\n\t\t\t\u00a0 @var2 DECIMAL(10,2) = -123.45;\n\t\t\t\u00a0\n\t\t\tSELECT @var1 AS var1, @var2 AS var2;\n\t\t<\/pre>\n<p>All we&#8217;ve done here is to designate our two literal values as positive and negative numbers. The <code>SELECT<\/code> statement returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>123.45<\/p>\n<\/td>\n<td valign=\"top\">\n<p>-123.45<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Be aware, however, that you cannot use the positive unary operator to convert a negative number to a positive number. For that, you need to use the <code>ABS<\/code> mathematical function.<\/p>\n<\/div>\n<h3 id=\"fourth\">&#8220;The NULLIF expression makes no sense. I would think you&#8217;d use it to somehow ferret out a NULL value or to test a condition for a NULL value. But it appears to merely return a NULL if two values are equal. Is that all it&#8217;s doing?&#8221;<\/h3>\n<div class=\"indented\">\n<p>That&#8217;s part of what it&#8217;s doing. When a <code>NULLIF<\/code> expression compares two values, it returns the first value if the two values are not equal. If they are equal, the expression returns a <code>NULL<\/code> value of the same data type as the first specified value. Here&#8217;s what a <code>NULLIF<\/code> expression looks like in action:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 VARCHAR(20) = 'valueA',\n\t\t\t\u00a0 @var2 VARCHAR(20) = 'valueB';\n\t\t\t\u00a0\n\t\t\tSELECT NULLIF(@var1, @var2);\n\t\t<\/pre>\n<p>In this case, the <code>NULLIF<\/code> expression returns <code>valueA<\/code> because the two values are not equal. If they were equal, the returned value would be <code>NULL<\/code>. In addition, the returned value would be <code>NULL<\/code> if the first value were <code>NULL<\/code>.<\/p>\n<p>When you know how a <code>NULLIF<\/code> expression works, it doesn&#8217;t seem so bad, although its use still has a tendency to cause confusion. Luckily, we can achieve the same results by using a <code>CASE<\/code> statement:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 VARCHAR(20) = 'valueA',\n\t\t\t\u00a0 @var2 VARCHAR(20) = 'valueB';\n\t\t\t\u00a0\n\t\t\tSELECT \n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN @var1 = @var2 THEN NULL\n\t\t\t\u00a0\u00a0\u00a0 ELSE @var1\n\t\t\t\u00a0 END;\n\t\t<\/pre>\n<p>The advantage of the <code>CASE<\/code> statement is that it&#8217;s a lot easier to understand. Sure, <code>NULLIF<\/code> provides simpler syntax, but if it creates confusion, what&#8217;s the point?<\/p>\n<\/div>\n<h3 id=\"fifth\">&#8220;Can you use expressions in data manipulation language (DML) statements?&#8221;<\/h3>\n<div class=\"indented\">\n<p>You can use expressions wherever T-SQL syntax permits them, including DML statements. Take a look at part of the syntax for the <code>UPDATE<\/code> statement&#8217;s <code>SET<\/code> clause:<\/p>\n<pre>SET { column_name = { expression | DEFAULT | NULL }<\/pre>\n<p>\u00a0According to SQL Server Books Online, the expression placeholder can be a &#8220;variable, literal value, expression, or a subselect statement (enclosed with parentheses) that returns a single value.&#8221; This pretty much defines the term <i>expression<\/i> as we understand it. Note, however, that any expression you define must conform to the rules associated with the particular T-SQL statement in which the expression is defined. For example, an <code>UPDATE<\/code> statement&#8217;s <code>UPDATE<\/code> clause can include the <code>TOP<\/code> clause, as shown in the following syntax:<\/p>\n<pre>UPDATE [ TOP ( expression ) [ PERCENT ] ]<\/pre>\n<p>\u00a0In this case, expression &#8220;specifies the number or percent of rows that will be updated.&#8221; In other words, you cannot specify a non-numerical string value as your expression, as you can with other types of expressions. That said, it&#8217;s still considered an expression.<\/p>\n<p>So, yes, you can use an expression in a DML statement. In fact, they&#8217;re used all the time. Let&#8217;s look at an example. Suppose we create the following table in our database and insert a row of data:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tIF OBJECT_ID('Inventory', 'U') IS NOT NULL\n\t\t\tDROP TABLE Inventory;\n\t\t\tGO\n\t\t\t\u00a0\n\t\t\tCREATE TABLE Inventory\n\t\t\t(\n\t\t\t\u00a0 ProductID INT NOT NULL IDENTITY PRIMARY KEY,\n\t\t\t\u00a0 InStock INT NOT NULL,\n\t\t\t\u00a0 OnOrder INT NOT NULL\n\t\t\t);\n\t\t\tGO\n\t\t\t\u00a0\n\t\t\tINSERT INTO Inventory(InStock, OnOrder)\n\t\t\tVALUES(20, 50);\n\t\t<\/pre>\n<p>Now let&#8217;s use an <code>UPDATE<\/code> statement to modify that row of data:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE \n\t\t\t\u00a0 @AmtReceived INT = 47;\n\t\t\t\u00a0\n\t\t\tUPDATE Inventory\n\t\t\tSET\n\t\t\t\u00a0 InStock = InStock + @AmtReceived,\n\t\t\t\u00a0 OnOrder = OnOrder - @AmtReceived\n\t\t\tWHERE ProductID = 1;\n\t\t\t\u00a0\n\t\t\tSELECT * FROM Inventory\n\t\t\tWHERE ProductID = 1;\n\t\t<\/pre>\n<p>Our <code>SET<\/code> clause is updating two columns. Each <code>SET<\/code> definition includes an expression that either adds the <code>@AmtReceived<\/code> value to the target column or subtracts that value. If we run the <code>SELECT<\/code> statement after the table has been updated, we come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>InStock<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>OnOrder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>67<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you&#8217;re writing T-SQL statements, you&#8217;re writing expressions, even if you don&#8217;t realize it. That&#8217;s why it&#8217;s important to refer back to the statement&#8217;s syntax whenever you&#8217;re uncertain how to define a particular element. Expressions are supported throughout most statements. The syntax for the <code>UPDATE<\/code> statement, for example, includes the expression placeholder in 10 different places, which suggests that many of us might not be using some statements to their fullest capacity.<\/p>\n<\/div>\n<h3 id=\"sixth\">\u00a0&#8220;The CASE expression allows me to leave out the ELSE clause. Is it that important to include it?&#8221;<\/h3>\n<div class=\"indented\">\n<p>Yes, but&#8230;. The <code>ELSE<\/code> clause specifies the value to return if no other comparison operations (<code>WHEN...THEN<\/code>) evaluate to true. If you omit the <code>ELSE<\/code> clause, the database engine instead returns a <code>NULL<\/code> value. A <code>NULL<\/code> value might be fine, if that&#8217;s what you want. But what if it&#8217;s not? Take a look at the following example, which uses a <code>CASE<\/code> expression to determine how to return an employee&#8217;s full name:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 BusinessEntityID AS EmployeeID,\n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN MiddleName IS NULL THEN FirstName + ' ' + LastName\n\t\t\t\u00a0\u00a0\u00a0 WHEN LEN(MiddleName) = 1 THEN FirstName + ' ' + MiddleName + '. ' + LastName\n\t\t\t\u00a0\u00a0\u00a0 ELSE FirstName + ' ' + MiddleName + ' ' + LastName\n\t\t\t\u00a0 END AS FullName,\n\t\t\t\u00a0 TerritoryName AS Territory\n\t\t\tFROM Sales.vSalesPerson\n\t\t\tWHERE TerritoryName IN ('central', 'northeast', 'southeast');\n\t\t<\/pre>\n<p>If the <code>MiddleName<\/code> column is <code>NULL<\/code>, that column is not included in the expression. Only the first and last names are concatenated. If the <code>MiddleName<\/code> value is an initial, a period is added to the initial and concatenated with the first and last names. Otherwise, all three names are concatenated, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now let&#8217;s modify the <code>CASE<\/code> expression by removing the <code>ELSE<\/code> clause:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 BusinessEntityID AS EmployeeID,\n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN MiddleName IS NULL THEN FirstName + ' ' + LastName\n\t\t\t\u00a0\u00a0\u00a0 WHEN LEN(MiddleName) = 1 THEN FirstName + ' ' + MiddleName + '. ' + LastName\n\t\t\t\u00a0 END AS FullName,\n\t\t\t\u00a0 TerritoryName AS Territory\n\t\t\tFROM Sales.vSalesPerson\n\t\t\tWHERE TerritoryName IN ('central', 'northeast', 'southeast');\n\t\t<\/pre>\n<p>This time a <code>NULL<\/code> is returned if the <code>MiddleName<\/code> column has a value that is more than an initial, as the following table shows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly, you would not want to delete the <code>ELSE<\/code> clause in this instance. You could, however, add a <code>WHEN...THEN<\/code> condition that replaces the <code>ELSE<\/code> clause, in which case, you&#8217;re still returning <code>NULL<\/code> of none of the conditions are met, which might be fine under some circumstances. Consider the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 FirstName + ' ' + LastName AS FullName,\n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN SalesQuota &gt; 250000 THEN 'true'\n\t\t\t\u00a0\u00a0\u00a0 WHEN SalesQuota &lt;= 250000 THEN 'false'\n\t\t\t\u00a0 END AS HigherQuota\n\t\t\tFROM Sales.vSalesPerson\n\t\t\tWHERE CountryRegionName = 'United States';\n\t\t<\/pre>\n<p>If the <code>SalesQuota<\/code> value is great than 250,000, a <code>true<\/code> is returned. If the value is less than or equal to 250,000, we get a <code>false<\/code>. However, if the column is <code>NULL<\/code>, neither condition can be true, so the <code>CASE<\/code> expression returns a <code>NULL<\/code>, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>HigherQuota<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Stephen Jiang<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Syed Abbas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Amy Alberts<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can choose to include an <code>ELSE<\/code> clause and then provide a default value, such as <code>N\/A<\/code> (or even <code>NULL<\/code>), whether or not you think it&#8217;s necessary. In fact, many developers think you should never omit the <code>ELSE<\/code> clause, even if you believe it is impossible for one of the <code>WHEN...THEN<\/code> conditions not to be met. Someone might make a change to the database that affects the statement or your thinking about possible outcomes might not be correct.<\/p>\n<\/div>\n<h3 id=\"seventh\">&#8220;Can you shed light on the order in which the database engine processes the components in an expression?&#8221;<\/h3>\n<div class=\"indented\">\n<p>The way in which an expression&#8217;s elements are processed depends on several factors: their listed order, whether any subset of elements is enclosed in parentheses, and what operators are used to connect the various elements. Parentheses group elements together to ensure that those elements are processed as a unit before being incorporated into the rest of the expression. In addition, the expression as a whole adheres to the concept of <i>operator precedence,<\/i> in which the operator types determine the order in which the expression is evaluated. For example, the multiply and division arithmetic operators take precedence over the greater than (<code>&gt;<\/code>) and lesser than (<code>&lt;<\/code>) comparison operations, which take precedence over logical operators such as <code>AND<\/code> or <code>ANY<\/code>. (You can find details about operator precedence in the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190276.aspx\">Operator Precedence<\/a>.&#8221;)<\/p>\n<p>Let&#8217;s start with a few simple arithmetic-based expressions to give you a sense of how this works. The following example declares several <code>INT<\/code> variables, assigns an expression to each one, and returns the values from all three:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @num1 INT = 3 * 2 + 5 - 7;\n\t\t\tDECLARE @num2 INT = 3 * (2 + 5) - 7;\n\t\t\tDECLARE @num3 INT = 3 * (2 + 5 - 7);\n\t\t\t\u00a0\n\t\t\tSELECT \n\t\t\t\u00a0 @num1 AS num1,\n\t\t\t\u00a0 @num2 AS num2,\n\t\t\t\u00a0 @num3 AS num3;\n\t\t<\/pre>\n<p>All we&#8217;re doing in each expression is multiplying and adding and subtracting values. However, because we use parentheses in the second and third expressions, our results come up quite different:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>num1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>@num1<\/code> expression is fairly straightforward. We multiple 3 by 2, add 5, and subtract 7, which gives us a total of 4.<\/p>\n<p>However, in the <code>@num2<\/code> expression, we enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> elements in parentheses, so they&#8217;re processed first, giving us a total of 7. The 7 is then multiplied by the first 3, giving us 21. From there, we subtract 7, giving us a total of 14.<\/p>\n<p>In the <code>@num3<\/code> expression, the parentheses enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> <code>-<\/code> <code>7<\/code> elements, which are processed before the other elements. Because this returns 0, 3 is multiplied by 0, giving us a final result of 0.<\/p>\n<p>Now let&#8217;s look at another example, which again declares three variables and applies different expressions to them:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @SalesLastYear INT = 2500;\n\t\t\tDECLARE @SalesYTD INT = 1500;\n\t\t\tDECLARE @SalesQuota INT = 3000;\n\t\t\t\u00a0\n\t\t\tSELECT \n\t\t\t\u00a0 @SalesQuota * 2 - @SalesLastYear + @SalesYTD * 2 AS SalesDiff1,\n\t\t\t\u00a0 (@SalesQuota * 2) - (@SalesLastYear + (@SalesYTD * 2)) AS SalesDiff2;\n\t\t<\/pre>\n<p>Here&#8217;s what are results look like:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>SalesDiff1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesDiff2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6500<\/p>\n<\/td>\n<td valign=\"top\">\n<p>500<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0The first expression includes no parentheses. However, the multiply operator takes precedence over the addition and subtraction operators, so the multiplication is performed first. That means we first multiply the <code>SalesQuota<\/code> value by 2 and the <code>SalesYTD<\/code> value by 2, giving us the expression <code>6000<\/code> <code>-<\/code> <code>2500<\/code> <code>+<\/code> <code>3000<\/code>, which equals 6500.<\/p>\n<p>The second expression does use parentheses. The first set of parentheses returns a value of 6000. The second set of parentheses actually contains a third set, and those are the elements evaluated first, before the outer set of elements. After they&#8217;re evaluated, that part of the expression enclosed in the second set of parentheses becomes <code>@SalesLastYear<\/code> <code> +<\/code> <code>3000<\/code>, giving us a total of 5500. That 5500 is then subtracted from the 6000, which is how we end up with 500.<\/p>\n<p>Now let&#8217;s look at an expression that incorporates character data as well as numerical data. In the following <code>SELECT<\/code> statement, our <code>WHERE<\/code> clause includes both the <code>OR<\/code> and the <code>AND<\/code> logical operators:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 StandardCost,\n\t\t\t\u00a0 ListPrice,\n\t\t\t\u00a0 ProductLine\n\t\t\tFROM Production.Product\n\t\t\tWHERE ListPrice &gt; StandardCost * 2.5\n\t\t\t\u00a0 AND ProductLine = 'r' OR ProductLine = 't';\n\t\t<\/pre>\n<p>The <code>WHERE<\/code> clause search condition contains three predicates separated by the <code>AND<\/code> operator and the <code>OR<\/code> operator. In its current state, the search condition states that the <code>ListPrice<\/code> value must be greater than 2.5 times the <code>StandardCost<\/code> value <i>and<\/i> the <code>ProductLine<\/code> value must equal <code>r<\/code> <i>or<\/i> the <code>ProductLine<\/code> value must equal <code>t<\/code>. According to the rules of operator precedence, the database engine first evaluates the <code>AND<\/code> logical operator and then the <code>OR<\/code> logical operator. Because of the placement of the operators, the first two predicates must both evaluate to true <i>or<\/i> the third predicate must evaluate to true. In other words, all returned rows must meet the first two conditions or meet the third condition. As a result, the statement returns 59 rows that qualify. The following table shows part of the results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Front Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>96.7964<\/p>\n<\/td>\n<td valign=\"top\">\n<p>218.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>108.7844<\/p>\n<\/td>\n<td valign=\"top\">\n<p>245.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-Panniers, Large<\/p>\n<\/td>\n<td valign=\"top\">\n<p>51.5625<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Touring Frame &#8211; Yellow, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>199.8519<\/p>\n<\/td>\n<td valign=\"top\">\n<p>333.42<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Suppose what we were really after was to return rows that met the first condition <i>and<\/i> either the second <i>or<\/i> third condition. In this case, we would have to modify our search condition slightly:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 StandardCost,\n\t\t\t\u00a0 ListPrice,\n\t\t\t\u00a0 ProductLine\n\t\t\tFROM Production.Product\n\t\t\tWHERE ListPrice &gt; StandardCost * 2.5\n\t\t\t\u00a0 AND (ProductLine = 'r' OR ProductLine = 't');\n\t\t<\/pre>\n<p>Notice that the second and third predicates are now enclosed in parentheses. Consequently, the database engine will first evaluate them as a unit. That means for all rows returned, <code>ListPrice<\/code> must be greater than 2.5 times the value of <code>StandardCost<\/code> <i>and<\/i> <code>ProductLine<\/code> must equal <code>r<\/code> <i>or<\/i> <code>t<\/code>. The following table shows all the results now returned by the query.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.4923<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.8663<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.0373<\/p>\n<\/td>\n<td valign=\"top\">\n<p>21.49<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ML Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>9.3463<\/p>\n<\/td>\n<td valign=\"top\">\n<p>24.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12.1924<\/p>\n<\/td>\n<td valign=\"top\">\n<p>32.60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.8423<\/p>\n<\/td>\n<td valign=\"top\">\n<p>28.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Notice that we now have only nine rows and that each row meets both conditions for the <code>ListPrice<\/code> and <code>ProductLine<\/code> columns. By applying the parentheses, we have better controlled how the rules of operator precedence are applied.<\/p>\n<\/div>\n<h3 id=\"eighth\">&#8220;I get confused on how to treat constants in my expressions. Is there a trick in using them?&#8221;<\/h3>\n<div class=\"indented\">\n<p>A constant is a literal value that is either part of an expression or is the entire expression. The way in which you treat constants depends on the type of data. As a general rule, you enclose character data in single quotes, but leave numerical data without quotes, although it&#8217;s not quite as straightforward as this. Before we go into the oddities, though, let&#8217;s look at a few examples of character constants. The following T-SQL declares several character variables and then retrieves their values:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 VARCHAR(20) = 'some value',\n\t\t\t\u00a0 @var2 VARCHAR(20) = '',\n\t\t\t\u00a0 @var3 NVARCHAR(20) = N'some value + 1',\n\t\t\t\u00a0 @var4 NVARCHAR(20) = N'12345';\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 AS var1,\n\t\t\t\u00a0 @var2 AS var2,\n\t\t\t\u00a0 @var3 AS var3,\n\t\t\t\u00a0 @var4 AS var4;\n\t\t<\/pre>\n<p>By default, when you&#8217;re working with character string constants, you enclose them in singe quotes. If you&#8217;re working with Unicode strings, you precede the opening quote with an uppercase N. Anything enclosed in the quotes is considered part of that string, even if the characters are numbers, operators, or other types of symbols, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>some value<\/p>\n<\/td>\n<td valign=\"top\">\u00a0<\/td>\n<td valign=\"top\">\n<p>some value + 1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12345<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0In SQL Server, we can instead enclose character strings in double quotes by setting the <code>QUOTED_IDENTIFIER<\/code> option to <code> OFF<\/code>, as shown in the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSET QUOTED_IDENTIFIER OFF;\n\t\t\tGO\n\t\t\t\u00a0\n\t\t\tDECLARE\n\t\t\t\u00a0 @var1 VARCHAR(20) = \"its value\",\n\t\t\t\u00a0 @var2 VARCHAR(20) = \"it's a value\",\n\t\t\t\u00a0 @var3 VARCHAR(20) = 'it''s a value';\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 AS var1,\n\t\t\t\u00a0 @var2 AS var2,\n\t\t\t\u00a0 @var3 AS var3;\n\t\t\t\u00a0\n\t\t\tSET QUOTED_IDENTIFIER ON;\n\t\t\tGO\n\t\t<\/pre>\n<p>When the <code>QUOTED_IDENTIFIER<\/code> option is set to <code>OFF<\/code>, we can use double quotes just like single quotes, as is the case with the <code>@var1<\/code> declaration. The advantage of using double quotes is that you can pass in a special character, such as an apostrophe, without having to escape it by adding a second apostrophe, as we had to do for <code>@var3<\/code>. The <code>SELECT<\/code> statement now returns the results shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>its value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Be aware, however, that the <code>QUOTED_IDENTIFIER<\/code> option has other implications besides being able to use double quotes for string constants. For example, the option affects the way identifiers are handled in T-SQL. In addition, the SQL Server Native Client Provider and ODBC driver set this option to <code>ON<\/code>, which can impact your code if you had set the option to <code>OFF<\/code>. For these reasons you&#8217;re usually better off leaving the option set to <code>ON<\/code>.<\/p>\n<p>As previously mentioned, you typically don&#8217;t enclose numerical constants in quotes, even if they include currency symbols or scientific notation. For example, the following T-SQL declares four types of numerical variables, sets their values, and retrieves those values:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 INT = 147,\n\t\t\t\u00a0 @var2 DECIMAL(10, 4) = 10.58,\n\t\t\t\u00a0 @var3 MONEY = $68.24,\n\t\t\t\u00a0 @var4 FLOAT = 87.9e;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 AS var1,\n\t\t\t\u00a0 @var2 AS var2,\n\t\t\t\u00a0 @var3 AS var3,\n\t\t\t\u00a0 @var4 AS var4;\n\t\t<\/pre>\n<p>If we had enclosed the constants in quotes, the database engine would have interpreted the values as character data and have tried to convert it to the type of the applicable variable. For the first three variables, an implicit conversion would have worked fine, but it would mean unnecessary work on the part of the database engine. For the last variable, the engine would have returned the following error:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tMsg 8114, Level 16, State 5, Line 2\n\t\t\tError converting data type varchar to float.\n\t\t<\/pre>\n<p>When we specify a numerical constant without quotes, the database engine assumes a numerical type consistent with the value. For example, the value <code>147<\/code> is assumed to be an <code>INT<\/code>, so no implicit conversion is necessary. The following table shows the results returned by the <code> SELECT<\/code> statement.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>147<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.5800<\/p>\n<\/td>\n<td valign=\"top\">\n<p>68.24<\/p>\n<\/td>\n<td valign=\"top\">\n<p>87.9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Even with numeric constants, an implicit conversion might be called for. In the following <code>SELECT<\/code> statement, the constant <code>1000<\/code> is compared the value in the <code>ListPrice<\/code> column, which is configured with the <code>MONEY<\/code> data type:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 ListPrice\n\t\t\tFROM Production.Product\n\t\t\tWHERE ListPrice &gt; 1000\n\t\t\t\u00a0 AND Color = 'blue';\n\t\t<\/pre>\n<p>The <code>1000<\/code> is treated as an <code>INT<\/code> value, so it must be implicitly converted to the <code>MONEY<\/code> type. But that&#8217;s not all what&#8217;s going on in the <code>WHERE<\/code> clause. Notice that it also includes a string constant (<code>blue<\/code>). The constant is compared to the <code>Color<\/code> column, which is configured with the <code>NVARCHAR<\/code> data type. This means that the database engine must convert the <code>VARCHAR<\/code> string to the <code>NVARCHAR<\/code> type, which gives us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Like string constants, date and time values are also enclosed in single quotes, even though the values are stored as integers, as you can see in the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 DATETIME = 'August 1, 2014',\n\t\t\t\u00a0 @var2 DATETIME = '2014-07-28 16:24:27.293',\n\t\t\t\u00a0 @var3 DATE = '7\/30\/2014',\n\t\t\t\u00a0 @var4 TIME = '14:05:55.001';\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 AS var1,\n\t\t\t\u00a0 @var2 AS var2,\n\t\t\t\u00a0 @var3 AS var3,\n\t\t\t\u00a0 @var4 AS var4;\n\t\t<\/pre>\n<p>The date and time data types will accept various formats when passing in a constant, as long as the value is enclosed in quotes. The following table shows the results returned by the <code>SELECT<\/code> statement:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2014-08-01 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-28 16:24:27.293<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-30<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14:05:55.0010000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0You should also be aware of how to treat constants when working with some of the other data types. For example, a <code>VARBINARY<\/code> value takes an <code>0x<\/code> prefix but is not enclosed in quotes. The same goes for a <code>BIT<\/code> value. However, a <code>UNIQUEIDENTIFIER<\/code> value is enclosed in quotes, as shown in the following T-SQL:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 VARBINARY(20) = 0x14Ad,\n\t\t\t\u00a0 @var2 BIT = 1,\n\t\t\t\u00a0 @var3 UNIQUEIDENTIFIER = 'CC421A37-E462-4AE0-8451-38F837FC5A1A';\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 AS var1,\n\t\t\t\u00a0 @var2 AS var2,\n\t\t\t\u00a0 @var3 AS var3;\n\t\t<\/pre>\n<p>\u00a0Our <code>SELECT<\/code> statement now returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>0x14AD<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CC421A37-E462-4AE0-8451-38F837FC5A1A<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Again, we generally enclose string values in quotes and leave the quotes off for numerical values, but as you&#8217;ve seen, some areas can be somewhat gray. If you get confused, check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179899.aspx\">Constants<\/a>.&#8221;<\/p>\n<\/div>\n<h3 id=\"ninth\">&#8220;Are there any advantages to using a COALESCE expression rather than a CASE expression when checking for the first non-NULL value in a list of values?&#8221;<\/h3>\n<div class=\"indented\">\n<p>A <code>COALESCE<\/code> expression is a syntactic shortcut for a <code>CASE<\/code> expression. That means, when the query optimizer gets ahold of a <code>COALESCE<\/code> expression, it rewrites it as a <code>CASE<\/code> one, so in that sense, they&#8217;re one in the same. Let&#8217;s look at a couple of examples. In the first one, we use <code>COALESCE<\/code> to return the first column that is not a <code>NULL<\/code> value:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 Size,\n\t\t\t\u00a0 SizeUnitMeasureCode AS SizeUnit,\n\t\t\t\u00a0 WeightUnitMeasureCode AS WeightUnit,\n\t\t\t\u00a0 COALESCE(Size, SizeUnitMeasureCode, WeightUnitMeasureCode) AS FirstNotNull\n\t\t\tFROM Production.Product\n\t\t\tWHERE ProductID = 828;\n\t\t<\/pre>\n<p>For this particular product, the <code>Size<\/code> column and <code>SizeUnitMeasureCode<\/code> column each contains a <code>NULL<\/code> value, but the <code>WeightUnit<\/code> column contains an actual value, <code>G<\/code>, so the <code>COALESCE<\/code> expression returns that value, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Size<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SizeUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>WeightUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FirstNotNull<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G\u00a0<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G\u00a0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Now let&#8217;s look at what happens when we change the <code> COALESCE<\/code> expression to a <code>CASE<\/code> expression:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 Size,\n\t\t\t\u00a0 SizeUnitMeasureCode AS SizeUnit,\n\t\t\t\u00a0 WeightUnitMeasureCode AS WeightUnit,\n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN Size IS NOT NULL THEN Size\n\t\t\t\u00a0\u00a0\u00a0 WHEN SizeUnitMeasureCode IS NOT NULL THEN SizeUnitMeasureCode\n\t\t\t\u00a0\u00a0\u00a0 WHEN WeightUnitMeasureCode IS NOT NULL THEN WeightUnitMeasureCode\n\t\t\t\u00a0\u00a0\u00a0 ELSE NULL\n\t\t\t\u00a0 END AS FirstNotNull\n\t\t\tFROM Production.Product\n\t\t\tWHERE ProductID = 828;\n\t\t<\/pre>\n<p>The <code>SELECT<\/code> statement returns the same results as the previous example. That said, the <code>COALESCE<\/code> expression is a lot simpler, although the logic of the <code>CASE<\/code> statement is easier to understand. In some cases, you might find that it&#8217;s better to use <code>CASE<\/code> so your intent is clear to other developers.<\/p>\n<p>One other consideration with <code>COALESCE<\/code>. Developers sometimes run into problems with <code>COALESCE<\/code> expressions because of data type issues. The value returned by <code>COALESCE<\/code> is based on the data type of the input value with the highest precedence, which might or might not be the returned value. If an implicit conversion is required, and such a conversion is not permitted, <code>COALESCE<\/code> will return an error. See the article &#8220;<a href=\"https:\/\/www.simple-talk.com\/sql\/t-sql-programming\/questions-about-sql-server-data-types-you-were-too-shy-to-ask\/\">Questions about SQL Server Data Types You were Too Shy to Ask<\/a>&#8221; for more details about this particular issue.<\/p>\n<p>All that said, figuring out the logic behind <code>COALESCE<\/code> is not too difficult, so choosing between <code>CASE<\/code> and <code>COALESCE<\/code> often comes down to nothing more than personal preference.<\/p>\n<\/div>\n<h3 id=\"tenth\">&#8220;I&#8217;m working on a query that uses the NOT IN operator in the WHERE clause. The operator checks values in a list returned by a subquery. When the returned list includes a NULL value, the query returns an empty resultset, even though I know I should be seeing results. Any idea what might be happening?&#8221;<\/h3>\n<div class=\"indented\">\n<p>Using <code>NOT<\/code> <code>IN<\/code> can be tricky, especially when <code>NULL<\/code> values are involved. Case in point. The following <code>SELECT<\/code> statement includes a <code>WHERE<\/code> clause that checks for specific colors:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT Name AS ProductName, Color\n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 1\n\t\t\t\u00a0 AND Color NOT IN\n\t\t\t\u00a0\u00a0\u00a0 (SELECT DISTINCT Color FROM Production.Product\n\t\t\t\u00a0\u00a0\u00a0\u00a0 WHERE FinishedGoodsFlag = 0); \n\t\t<\/pre>\n<p>Basically, what we&#8217;re doing is returning all products that are saleable items (<code>FinishedGoodsFlag<\/code> <code>=<\/code> <code>1<\/code>) and whose color is not one of the colors of the non-saleable products. Unfortunately, when we run this query we receive an empty resultset.<\/p>\n<p>If you&#8217;re familiar with the data, you might be surprised by these results. After all, the table contains 295 rows of saleable products. Either there are no colors unique to saleable items, or something is wrong.<\/p>\n<p>We can investigate the problem by first running the subquery in the example above separate from the outer query:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT DISTINCT Color \n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 0; \n\t\t<\/pre>\n<p>The following table shows the subquery&#8217;s results, which includes three distinct items:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, we have black and silver non-saleable items, along with those items for which no color has been assigned (the <code>NULL<\/code> value). Now let&#8217;s modify the subquery to instead retrieve the colors of the orderable products:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT DISTINCT Color \n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 1; \n\t\t<\/pre>\n<p>This time around, we again receive the same three values, along with another seven colors, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Grey<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver\/Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Yellow<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly something is wrong. Rows that contain those seven additional colors should have been included in our original query results. The problem, it turns out, is the <code>NULL<\/code> value. Let&#8217;s recast the logic of the <code>WHERE<\/code> clause into several separate predicates, connected by the <code>AND<\/code> logical operator:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT Name AS ProductName, Color\n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 1\n\t\t\t\u00a0 AND Color &lt;&gt; 'Black'\n\t\t\t\u00a0 AND Color &lt;&gt; 'Silver'\n\t\t\t\u00a0 AND Color &lt;&gt; NULL; \n\t\t<\/pre>\n<p>Essentially, our <code>NOT<\/code> <code>IN<\/code> operator indicates that the value in the <code>Color<\/code> column should not be <code>Black<\/code> <i>and<\/i> should not be <code>Silver<\/code> <i>and<\/i> should not be <code> NULL<\/code>, in addition to being a saleable product. The problem is with the equation <code>Color<\/code> <code>&lt;&gt;<\/code> <code>NULL<\/code>. For a row to be returned, all conditions must evaluate to true; however, a <code>NULL<\/code> cannot evaluate to true or false, which means the condition as a whole will never evaluate to true and no rows will ever be returned. If we were to run this statement, we would once again receive an empty resultset.<\/p>\n<p>One way around this predicament is to add a second predicate to the <code>WHERE<\/code> clause in our subquery:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT Name AS ProductName, Color\n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 1\n\t\t\t\u00a0 AND Color NOT IN\n\t\t\t\u00a0\u00a0\u00a0 (SELECT DISTINCT Color FROM Production.Product\n\t\t\t\u00a0\u00a0\u00a0\u00a0 WHERE FinishedGoodsFlag = 0 AND Color IS NOT NULL); \n\t\t<\/pre>\n<p>Now our subquery returns only the values <code>Black<\/code> and <code>Silver<\/code>, which means our outer query should return all rows except those whose <code>Color<\/code> value is one of the two colors. The following table shows part of the results, although the query actually returns 120 rows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Blue<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AWC Logo Cap<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, S<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, XL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you were to run this query, you would see the <code>Color<\/code> values <code>black<\/code> and <code>silver<\/code> are not included. However, what&#8217;s odd about this is that rows with <code>NULL<\/code> for a color are not included either, even though <code>NULL<\/code> is no longer being returned by the subquery. This might be fine in some cases, but in others you might want to see those values. Before we start looking for a workaround, however, another consideration is performance. Many SQL Server folks recommend using an <code>EXISTS<\/code> operator rather than an <code>IN<\/code> operator anyway. So let&#8217;s rewrite the last statement using <code>NOT<\/code> <code>EXISTS<\/code>:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT Name AS ProductName, Color \n\t\t\tFROM Production.Product a\n\t\t\tWHERE a.FinishedGoodsFlag = 1 AND Color IS NOT NULL\n\t\t\t\u00a0 AND NOT EXISTS\n\t\t\t\u00a0\u00a0\u00a0 (SELECT * FROM Production.Product b\n\t\t\t\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WHERE FinishedGoodsFlag = 0 AND a.Color = b.Color); \n\t\t<\/pre>\n<p>Our <code>WHERE<\/code> clause now specifies that we include only saleable products, that they be a known color, and that the color not exist within the list of non-orderable colors. As a result, the <code>SELECT<\/code> statement returns the same results as the preceding one, 120 rows. And if we want to include the saleable products with a <code>Color<\/code> value of <code>NULL<\/code>, we can simply remove the <code>IS<\/code> <code>NOT<\/code> <code>NULL<\/code> expression:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT Name AS ProductName, Color \n\t\t\tFROM Production.Product a\n\t\t\tWHERE a.FinishedGoodsFlag = 1\n\t\t\t\u00a0 AND NOT EXISTS\n\t\t\t\u00a0\u00a0\u00a0 (SELECT * FROM Production.Product b\n\t\t\t\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WHERE FinishedGoodsFlag = 0 AND a.Color = b.Color); \n\t\t<\/pre>\n<p>Our statement now returns 170 rows, which include those rows with a <code>Color<\/code> value of <code>NULL<\/code>. In theory, this statement should perform better than the ones using <code>NOT<\/code> <code>NULL<\/code>, but you might want to test that part out yourself to be sure. Just know that using <code>NOT<\/code> <code>IN<\/code> with <code>NULL<\/code> values can return unexpected results, so be prepared.<\/p>\n<\/div>\n<h3 id=\"eleventh\">\u00a0&#8220;What the heck is a &#8216;modulo&#8217;?&#8221;<\/h3>\n<div class=\"indented\">\n<p>Remember when you first learned long division? You had the dividend, the divisor, and the remainder. The dividend is the number you start with. The divisor is the number you divide into the dividend. The remainder is what&#8217;s left. Modulo is a mathematical operator that returns the remainder.<\/p>\n<p>Here&#8217;s how it works. In the follow example, <code>@var1<\/code> serves as our dividend, and <code>@var2<\/code> serves as our divisor:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 INT = 32,\n\t\t\t\u00a0 @var2 INT = 8;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 \/ @var2 AS Division,\n\t\t\t\u00a0 @var1 % @var2 AS Remainder;\n\t\t<\/pre>\n<p>The first expression in our <code>SELECT<\/code> clause uses the division operator (<code>\/<\/code>) to divide <code>@var1<\/code> by <code>@var2<\/code>. The second expression uses the modulo operator (<code>%<\/code>) to calculate the remainder that&#8217;s produced when you divide these two numbers. The following table shows the results.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>No surprise here. When you divide 32 by 8, you get 4, with a remainder of 0. But now let&#8217;s produce a remainder other than 0:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 INT = 36,\n\t\t\t\u00a0 @var2 INT = 8;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 \/ @var2 AS Division,\n\t\t\t\u00a0 @var1 % @var2 AS Remainder;\n\t\t<\/pre>\n<p>This time around, we simply set <code>@var1<\/code> to equal 36, rather than 32. Now our results show a remainder of 4.<\/p>\n<p>That&#8217;s all there is to using the modulo operator. And you can use it with other numeric types:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE\n\t\t\t\u00a0 @var1 DECIMAL(10,4) = 55.556,\n\t\t\t\u00a0 @var2 INT = 5;\n\t\t\t\u00a0\n\t\t\tSELECT\n\t\t\t\u00a0 @var1 \/ @var2 AS Division,\n\t\t\t\u00a0 @var1 % @var2 AS Remainder;\n\t\t<\/pre>\n<p>In this case, we&#8217;re dividing 55.566 by 5 to come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>11.111200000000000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0.5560<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, the expression that contains the modulo operator returns a value of <code>0.5560<\/code>. As for the expression that contains the division operator, the operator&#8217;s returned value is based on the argument type with the highest precedence, which in this case is <code>DECIMAL<\/code>.<\/p>\n<\/div>\n<h3 id=\"twelveth\">&#8220;Are all WHEN and ELSE clauses in a CASE expression evaluated if the first WHEN clause evaluates to TRUE?&#8221;<\/h3>\n<div class=\"indented\">\n<p>According to the SQL Server documentation, a <code>CASE<\/code> expression evaluates each condition sequentially and stops with the first condition that evaluates to true. In other words, the expression will short circuit when a condition is met, saving the database engine from having to perform unnecessary processing. This is a good thing. The less work the engine has to perform, the better.<\/p>\n<p>To get a sense of how this works, let&#8217;s look at an example. The following <code>SELECT<\/code> statement includes a <code>CASE<\/code> expression that sets up three possible conditions when concatenating the name-related columns in the <code>vSalesPerson<\/code> view:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT \n\t\t\t\u00a0 BusinessEntityID AS EmployeeID,\n\t\t\t\u00a0 CASE\n\t\t\t\u00a0\u00a0\u00a0 WHEN MiddleName IS NULL THEN FirstName + ' ' + LastName\n\t\t\t\u00a0\u00a0\u00a0 WHEN LEN(MiddleName) = 1 THEN FirstName + ' ' + MiddleName + '. ' + LastName\n\t\t\t\u00a0\u00a0\u00a0 ELSE FirstName + ' ' + MiddleName + ' ' + LastName\n\t\t\t\u00a0 END AS FullName,\n\t\t\t\u00a0 TerritoryName AS Territory\n\t\t\tFROM Sales.vSalesPerson\n\t\t\tWHERE TerritoryName IN ('central', 'northeast', 'southeast');\n\t\t<\/pre>\n<p>The first <code>CASE<\/code> condition checks whether the <code>MiddleName<\/code> value is <code>NULL<\/code>. If it is, the <code>FirstName<\/code> and <code>LastName<\/code> columns are concatenated and the expression stops running. If the column contains a value other than <code>NULL<\/code>, the expression moves on to the next condition, which checks the length of the <code>MiddleName<\/code> value. If it equals <code>1<\/code>, the three columns are concatenated and a period is added to the middle initial. The expression will then stop at that point. Otherwise, the <code>ELSE<\/code> condition is applied and all three columns are concatenated as is. The database engine repeats the process for each row and returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>CASE<\/code> expression&#8217;s ability to short circuit can help to avoid unnecessary processing when it can be substituted for other statement elements. For example, suppose we define the following <code>SELECT<\/code> statement:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT\n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 Color\n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 1 AND\n\t\t\t\u00a0 (Color = 'black' OR color = 'red');\n\t\t<\/pre>\n<p>Nothing flashy here. For each row, the database engine tests for the three specified conditions and determines whether that row is returned. Consequently, the resultset includes any saleable product that is either black or red, which means 127 rows evaluate to true. Part of those results are shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 52<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 56<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0Suppose we want to reduce some of the work that the database engine has to perform. We can instead use a <code>CASE<\/code> expression in the <code>WHERE<\/code> clause, as shown in the following <code>SELECT<\/code> statement:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tSELECT\n\t\t\t\u00a0 Name AS ProductName,\n\t\t\t\u00a0 Color\n\t\t\tFROM Production.Product\n\t\t\tWHERE FinishedGoodsFlag = 1 AND\n\t\t\t\u00a0 Color = CASE Color\n\t\t\t\u00a0\u00a0\u00a0 WHEN 'black' THEN Color\n\t\t\t\u00a0\u00a0\u00a0 WHEN 'red' THEN Color\n\t\t\t\u00a0 END;\n\t\t<\/pre>\n<p>If the <code>Color<\/code> value equals <code>black<\/code>, the condition evaluates to true and the expression stops running, thus avoiding any unnecessary processing. Yet the statement returns the same results as the preceding example.<\/p>\n<p>Under the right circumstances, using a <code>CASE<\/code> expression in this way can be a handy way to reduce your workloads. However, a <code>CASE<\/code> expression might not always short circuit in the way you hope. For example, suppose your T-SQL includes logic is similar to that shown in the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 INT = 0;\n\t\t\t\u00a0\n\t\t\tSELECT \n\t\t\t\u00a0 CASE \n\t\t\t\u00a0\u00a0\u00a0 WHEN @var1 = 0 THEN 0 \n\t\t\t\u00a0\u00a0\u00a0 ELSE MAX(1\/0) \n\t\t\t\u00a0 END;\n\t\t<\/pre>\n<p>All we&#8217;re doing is declaring an <code>INT<\/code> variable, setting its value to <code>0<\/code>, and using a <code>CASE<\/code> expression to retrieve a specific value. Based on our understanding of the <code>CASE<\/code> expression&#8217;s ability to short circuit, we would expect the expression&#8217;s first condition to evaluate to true and the expression to then stop. However, what we get from our <code>SELECT<\/code> statement is the following error:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tMsg 8134, Level 16, State 1, Line 3\n\t\t\tDivide by zero error encountered.\n\t\t<\/pre>\n<p>The error shows that the <code>ELSE<\/code> condition did in fact run, even though it shouldn&#8217;t have. It turns out that under certain circumstances, a <code>CASE<\/code> expression doesn&#8217;t behave like it&#8217;s supposed to behave. The problem appears to be related to aggregate functions (at least in part), so be aware that in some cases a <code>CASE<\/code> expression might not deliver the benefits you&#8217;re hoping for and you might have to come up with a workaround to make the <code>CASE<\/code> expression work the way you want it to.<\/p>\n<\/div>\n<h3 id=\"thirteenth\">&#8220;What&#8217;s the difference between the two &#8216;not equal to&#8217; operators (&lt;&gt; and !=)?&#8221;<\/h3>\n<div class=\"indented\">\n<p>In T-SQL, the two comparisons operators are syntactically equivalent. They each perform a Boolean comparison between two expressions. If those expressions return non-<code>NULL<\/code>, unequal values, the comparison evaluates to true, otherwise, it evaluates to false. However, if either value is <code> NULL<\/code>, the evaluation returns a <code>NULL<\/code>. Here&#8217;s a sample of the two operators in action:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 INT = 1;\n\t\t\t\u00a0\n\t\t\tSELECT 'not equal' WHERE @var1 &lt;&gt; 2;\n\t\t\t\u00a0\n\t\t\tSELECT 'not equal' WHERE @var1 != 2;\n\t\t<\/pre>\n<p>\u00a0The variable is set to a value of <code> 1<\/code>, and in each <code>SELECT<\/code> statement, that variable is compared to the constant <code>2<\/code>. As a result, each <code>SELECT<\/code> statement returns the value <code>not<\/code> <code>equal<\/code> because each comparison evaluates to true. Even in terms of performance, there doesn&#8217;t appear to be much difference between the two.<\/p>\n<p>However, there is one difference you should be aware of. The <code>&lt;&gt;<\/code> operator conforms to ANSI standards; the <code>!=<\/code> operator does not, despite that fact that a number of relational database systems support both operators. That said, even if there is only the slightest chance that you might port your script to another system, you&#8217;re better off sticking with the <code> &lt;&gt;<\/code> operator.<\/p>\n<\/div>\n<h3 id=\"fourteenth\">&#8220;I&#8217;ve come across operators such as += in variable assignment SET statements. What do they mean?&#8221;<\/h3>\n<div class=\"indented\">\n<p>The add-equals operator (<code>+=<\/code>) is a compound operator, one of several types of compound operators supported in SQL Server. A compound operator takes the original value and in some way amends it, rather than replacing it. For example, the following T-SQL uses the add-equals operator to update the value of the <code>@var1<\/code> variable:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 INT = 2\n\t\t\t\u00a0\n\t\t\tSET @var1 += 1;\n\t\t\t\u00a0\n\t\t\tSELECT @var1;\n\t\t<\/pre>\n<p>We start by declaring <code>@var1<\/code> and setting its value to <code>2<\/code>. We then use a <code>SET<\/code> statement to add a value of <code>1<\/code> to the original value. The <code>SELECT<\/code> statement returns the new value, which is <code>3<\/code>.<\/p>\n<p>The add-equals operator makes it possible to add a value without having to specify the original value. Without the add-equals operator we would have to rewrite our <code>SET<\/code> statement as follows:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 INT = 2\n\t\t\t\u00a0\n\t\t\tSET @var1 = @var1 + 1;\n\t\t\t\u00a0\n\t\t\tSELECT @var1;\n\t\t<\/pre>\n<p>The add-equals operator merely provides the shorthand necessary to avoid having to explicitly specify the original value, yet the <code>SELECT<\/code> statement still returns a value of <code>3<\/code>. Plus, SQL Server supports other compound operators as well. For example, the following T-SQL subtracts <code>1<\/code> from the original value:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 INT = 2\n\t\t\t\u00a0\n\t\t\tSET @var1 -= 1;\n\t\t\t\u00a0\n\t\t\tSELECT @var1;\n\t\t<\/pre>\n<p>In this case, we&#8217;re using the subtract-equals (<code>-=<\/code>) operator in our <code>SELECT<\/code> statement, rather than the add-equals operator. The subtract-equals operator works the same way as the add-equals operator, except that the second value is subtracted rather than added. As a result, our <code>SELECT<\/code> statement now returns a value of <code>1<\/code>.<\/p>\n<p>Another example of a compound operator is the multiply-equals operator, which is shown in the following example:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 INT = 2\n\t\t\t\u00a0\n\t\t\tSET @var1 *= 3;\n\t\t\t\u00a0\n\t\t\tSELECT @var1;\n\t\t<\/pre>\n<p>In this case, the <code>SET<\/code> statement uses the multiply-equals operator to multiple the variable value <code>2<\/code> by the constant value <code>3<\/code>, giving us a total of <code>6<\/code>. And compound operators are not limited to mathematical expressions. For example, the following T-SQL includes a <code>SET<\/code> statement that uses the add-equals operator to concatenate values:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">\t\t\tDECLARE @var1 VARCHAR(30) = 'some value'\n\t\t\t\u00a0\n\t\t\tSET @var1 += ' and then some';\n\t\t\t\u00a0\n\t\t\tSELECT @var1;\n\t\t<\/pre>\n<p>In this example, the <code>SET<\/code> statement adds the string value <code>and<\/code> <code>then<\/code> <code>some<\/code> (including the opening space) to the variable value <code>some<\/code> <code>value<\/code>, giving us the value <code>some<\/code> <code>value<\/code> <code>and<\/code> <code>then<\/code> <code>some<\/code>. There are other compound operators as well, such as divide-equals (<code>\/=<\/code>) and modulo-equals (<code>%=<\/code>). Check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc645922.aspx\">Compound Operators<\/a>&#8221; for more information.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>+ CONVERT(NVARCHAR(20), CAST(@var1 AS MONEY), 1) AS amount1, &nbsp; &#8216;<\/p>\n\n\n\n<p>Each expression in the <code>SELECT<\/code> list references one of the variables as its source data and is made up of multiple components. What provides the final format, however, is the <code>CONVERT<\/code> function. The function requires that the source data be either the <code>MONEY<\/code> or <code>SMALLMONEY<\/code> type. When you call the function, you specify a second argument along with the source value. That argument determines the format of the data. In this case, we specify <code>1<\/code> so the outputted data includes commas every three digits to the left of the decimal point. We also concatenate the value with a dollar sign to give it that final touch, as shown in the following results:<\/p>\n\n\n\n<figure class=\"wp-block-table MsoTableGrid\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p><b>amount1<\/b><\/p>\n<\/td><td>\n<p><b>amount2<\/b><\/p>\n<\/td><td>\n<p><b>amount3<\/b><\/p>\n<\/td><\/tr><tr><td>\n<p>$555,555,555.56<\/p>\n<\/td><td>\n<p>$555,555,555.56<\/p>\n<\/td><td>\n<p>$555,555,555.56<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&nbsp;Overall, this isn&#8217;t too painful a process, unless you&#8217;re working with currencies that follow a different format, such as the Swiss franc, which takes apostrophes rather than commas. In such cases, you might have to add yet another function, such as <code>REPLACE<\/code>, to substitute the commas with apostrophes.<\/p>\n\n\n\n<p>Fortunately, since the release of SQL Server 2012, you&#8217;ve been able to instead use the <code>FORMAT<\/code> function to output your numbers to a currency format. The <code>FORMAT<\/code> function returns a value in a specified format, based on a specified culture. To demonstrate how this works, let&#8217;s first recast our preceding expressions into ones that use the <code>FORMAT<\/code> function to configure the data as US dollar amounts:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>Notice that we must first cast the character data (<code>@var1<\/code>) to the <code>MONEY<\/code> data type, but we can use the <code>DECIMAL<\/code> variable (<code>@var2<\/code>) as is. The second <code>FORMAT<\/code> argument (<code>c<\/code>) specifies that we&#8217;re formatting currency, and the third argument specifies the US symbol (<code>en-us<\/code>) for the cultural context. The <code>SELECT<\/code> statement returns the same results as the previous <code>SELECT<\/code> statement, with all values returned as the <code>NVARCHAR<\/code> type.<\/p>\n\n\n\n<p>However, with the <code>FORMAT<\/code> function, we can specify other cultures. For example, the following example uses the German symbol (<code>de-de<\/code>) to provide the cultural context:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>Now our results are specific to how the euro is represented in Germany, as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table MsoTableGrid\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p><b>amount1<\/b><\/p>\n<\/td><td>\n<p><b>amount2<\/b><\/p>\n<\/td><td>\n<p><b>amount3<\/b><\/p>\n<\/td><\/tr><tr><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&nbsp;Certainly, the <code>FORMAT<\/code> function is a handy tool if you want your results to be displayed in a specific currency. But just to reiterate, such formatting is generally handled by the calling application, with the source data retrieved in its raw state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"third\">&#8220;I see the plus sign (+) used in a variety of ways in T-SQL script, but I can&#8217;t always make sense of how SQL Server arrives at the results it does. Is there a &#8220;right way&#8221; to use plus signs?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>SQL Server is a tricky devil. As you&#8217;ve discovered, plus signs have all sorts of meaning in T-SQL. You can use them to concatenate string values, add numerical values together, or indicate that a numerical value is a positive number, rather than negative. The following <code>SELECT<\/code> statement demonstrates how to both concatenate and add values:<\/p>\n<pre wp-pre-tag-4=\"\"><\/pre>\n<p>For the <code>FullName<\/code> column, we&#8217;ve used the plus sign to concatenate the <code>FirstName<\/code> and <code> LastName<\/code> columns with a space in between. For the <code>TwoYearSales<\/code> column, we&#8217;ve used the plus sign to add the <code>SalesLastYear<\/code> and <code> SalesYTD<\/code> columns together. In both cases, the database engine knows when to concatenate the values or when to add them together, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesLastYear<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesYTD<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>TwoYearSales<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,750,406.48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,763,178.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,513,584.66<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,439,156.03<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,251,368.55<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,690,524.58<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,997,186.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,189,418.37<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,186,604.57<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Garrett Vargas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,620,276.90<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,453,719.47<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,073,996.36<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,849,640.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,315,185.61<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,164,826.55<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,927,059.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,352,577.13<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,279,636.31<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,073,506.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,458,535.62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,532,041.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jos\u00e9 Saraiva<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,038,234.65<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,604,540.72<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,642,775.37<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,371,635.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,573,012.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,944,648.25<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$0.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Lynn Tsoflias<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,278,548.98<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,421,810.92<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,700,359.90<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Rachel Valdez<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,307,949.79<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,827,066.71<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,135,016.50<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jae Pak<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,635,823.40<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,116,871.23<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,752,694.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Ranjit Varkey Chudukatil<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,396,539.76<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,121,616.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,518,156.08<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There&#8217;s no real mystery here. When we use the plus sign with only character data, the database engine concatenates the values. In this case, the plus sign is considered a string concatenation operator. When we use the plus sign with numerical values, the database engine adds the values together. Under these circumstances, the plus sign is considered an addition operator.<\/p>\n<p>Not surprisingly, when we mix and match the types of data, our results are less predictable. Take a look at the following example:<\/p>\n<pre wp-pre-tag-5=\"\"><\/pre>\n<p>As you can see, we&#8217;ve declared seven variables, each defined with a different type. We then use the plus sign to combine the variables in different ways, giving us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>TestVarCX<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1299.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1001.0001<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2015-05-21 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2017-04-19 23:45:36.000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As we would expect, when we pair <code>@var1<\/code> with <code>@var2<\/code>, both character types, we end up with a concatenated string. When we pair <code>@var3<\/code> to <code>@var6<\/code>, both numerical types, the values are added together, even though <code>@var3<\/code> and <code>@var6<\/code> are different types.<\/p>\n<p>Now we get to our third pairing: <code>@var4<\/code> <code>+<\/code> <code>@var5<\/code>. This time around, we&#8217;re adding a <code>DECIMAL<\/code> value to <code>BIT<\/code> value. However, the database engine still treats both variables as numerical values and adds them together, even though SQL Server documentation suggests that you can&#8217;t do this with the <code>BIT<\/code> type. (And really, why would you?)<\/p>\n<p>The next item in the select list, <code>@var7<\/code> <code>+<\/code> <code>@var3<\/code>, pairs a <code>DATETIME<\/code> value with a <code>SMALLINT<\/code> value. This time, we get a date that is 300 days later than the original date. When your expression includes the plus sign, the database engine defaults to the data type with the highest precedence, in this case, <code> DATETIME<\/code>. Based on the type, the engine determines whether to use the plus sign to concatenate values or add them together. If the data type with the highest precedence is a numeric data type, the engine attempts to add the values. In this case, the engine adds the <code>SMALLINT<\/code> value to the <code>DATETIME<\/code> value as the specified number of days. (A <code>DATETIME<\/code> value is actually stored as two integers.)<\/p>\n<p>Finally, we pair a <code>MONEY<\/code> date type with the <code>DATETIME<\/code> date type (<code>@var6<\/code> <code>+<\/code> <code>@var7<\/code>). Once again, <code>DATETIME<\/code> takes precedence over <code>MONEY<\/code>, so the database engine adds 999.99 days to our original date to come up with a date in 2017.<\/p>\n<p>We can verify how data type precedence works with plus signs by using the <code>SQL_VARIANT_PROPERTY<\/code> function to retrieve the data type of the expressions that pair the different variables:<\/p>\n<pre wp-pre-tag-6=\"\"><\/pre>\n<p>The <code>SELECT<\/code> statement returns the returned data type for each expression, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>money<\/p>\n<\/td>\n<td valign=\"top\">\n<p>decimal<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;In each case, the expression returns the type with the highest precedence and determines how the plus sign will be used.<\/p>\n<p>So far in our examples, the database engine has been able to add or concatenate values without a problem because the engine could implicitly convert the data type with the lowest precedence to the type with the highest. But this isn&#8217;t always the case:<\/p>\n<pre wp-pre-tag-7=\"\"><\/pre>\n<p>This time around, we&#8217;re using the plus sign to pair a <code>NVARCHAR<\/code> value with a <code>SMALLINT<\/code> value. Because <code>SMALLINT<\/code> has precedence over <code>NVARCHAR<\/code>, the database engine attempts to convert the value <code>TestVar<\/code> to the <code>SMALLINT<\/code> type, which of course is not possible. Consequently, the database engine returns the following error message:<\/p>\n<pre wp-pre-tag-8=\"\"><\/pre>\n<p>The only way we can pair a numerical value with a string value is to concatenate them. In order to do so, we must explicitly convert the <code>SMALLINT<\/code> value to a character data type:<\/p>\n<pre wp-pre-tag-9=\"\"><\/pre>\n<p>Now our <code>SELECT<\/code> statement returns the value <code>TestVar300<\/code>.<\/p>\n<p>There&#8217;s one other use of the plus sign that you should be aware of-as a unary plus operator. SQL Server supports two types of unary operators, plus and minus, which are used to designate whether a numeric value is positive or negative:<\/p>\n<pre wp-pre-tag-10=\"\"><\/pre>\n<p>All we&#8217;ve done here is to designate our two literal values as positive and negative numbers. The <code>SELECT<\/code> statement returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>123.45<\/p>\n<\/td>\n<td valign=\"top\">\n<p>-123.45<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Be aware, however, that you cannot use the positive unary operator to convert a negative number to a positive number. For that, you need to use the <code>ABS<\/code> mathematical function.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fourth\">&#8220;The NULLIF expression makes no sense. I would think you&#8217;d use it to somehow ferret out a NULL value or to test a condition for a NULL value. But it appears to merely return a NULL if two values are equal. Is that all it&#8217;s doing?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>That&#8217;s part of what it&#8217;s doing. When a <code>NULLIF<\/code> expression compares two values, it returns the first value if the two values are not equal. If they are equal, the expression returns a <code>NULL<\/code> value of the same data type as the first specified value. Here&#8217;s what a <code>NULLIF<\/code> expression looks like in action:<\/p>\n<pre wp-pre-tag-11=\"\"><\/pre>\n<p>In this case, the <code>NULLIF<\/code> expression returns <code>valueA<\/code> because the two values are not equal. If they were equal, the returned value would be <code>NULL<\/code>. In addition, the returned value would be <code>NULL<\/code> if the first value were <code>NULL<\/code>.<\/p>\n<p>When you know how a <code>NULLIF<\/code> expression works, it doesn&#8217;t seem so bad, although its use still has a tendency to cause confusion. Luckily, we can achieve the same results by using a <code>CASE<\/code> statement:<\/p>\n<pre wp-pre-tag-12=\"\"><\/pre>\n<p>The advantage of the <code>CASE<\/code> statement is that it&#8217;s a lot easier to understand. Sure, <code>NULLIF<\/code> provides simpler syntax, but if it creates confusion, what&#8217;s the point?<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fifth\">&#8220;Can you use expressions in data manipulation language (DML) statements?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>You can use expressions wherever T-SQL syntax permits them, including DML statements. Take a look at part of the syntax for the <code>UPDATE<\/code> statement&#8217;s <code>SET<\/code> clause:<\/p>\n<pre wp-pre-tag-13=\"\"><\/pre>\n<p>&nbsp;According to SQL Server Books Online, the expression placeholder can be a &#8220;variable, literal value, expression, or a subselect statement (enclosed with parentheses) that returns a single value.&#8221; This pretty much defines the term <i>expression<\/i> as we understand it. Note, however, that any expression you define must conform to the rules associated with the particular T-SQL statement in which the expression is defined. For example, an <code>UPDATE<\/code> statement&#8217;s <code>UPDATE<\/code> clause can include the <code>TOP<\/code> clause, as shown in the following syntax:<\/p>\n<pre wp-pre-tag-14=\"\"><\/pre>\n<p>&nbsp;In this case, expression &#8220;specifies the number or percent of rows that will be updated.&#8221; In other words, you cannot specify a non-numerical string value as your expression, as you can with other types of expressions. That said, it&#8217;s still considered an expression.<\/p>\n<p>So, yes, you can use an expression in a DML statement. In fact, they&#8217;re used all the time. Let&#8217;s look at an example. Suppose we create the following table in our database and insert a row of data:<\/p>\n<pre wp-pre-tag-15=\"\"><\/pre>\n<p>Now let&#8217;s use an <code>UPDATE<\/code> statement to modify that row of data:<\/p>\n<pre wp-pre-tag-16=\"\"><\/pre>\n<p>Our <code>SET<\/code> clause is updating two columns. Each <code>SET<\/code> definition includes an expression that either adds the <code>@AmtReceived<\/code> value to the target column or subtracts that value. If we run the <code>SELECT<\/code> statement after the table has been updated, we come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>InStock<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>OnOrder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>67<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you&#8217;re writing T-SQL statements, you&#8217;re writing expressions, even if you don&#8217;t realize it. That&#8217;s why it&#8217;s important to refer back to the statement&#8217;s syntax whenever you&#8217;re uncertain how to define a particular element. Expressions are supported throughout most statements. The syntax for the <code>UPDATE<\/code> statement, for example, includes the expression placeholder in 10 different places, which suggests that many of us might not be using some statements to their fullest capacity.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"sixth\">&nbsp;&#8220;The CASE expression allows me to leave out the ELSE clause. Is it that important to include it?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Yes, but&#8230;. The <code>ELSE<\/code> clause specifies the value to return if no other comparison operations (<code>WHEN...THEN<\/code>) evaluate to true. If you omit the <code>ELSE<\/code> clause, the database engine instead returns a <code>NULL<\/code> value. A <code>NULL<\/code> value might be fine, if that&#8217;s what you want. But what if it&#8217;s not? Take a look at the following example, which uses a <code>CASE<\/code> expression to determine how to return an employee&#8217;s full name:<\/p>\n<pre wp-pre-tag-17=\"\"><\/pre>\n<p>If the <code>MiddleName<\/code> column is <code>NULL<\/code>, that column is not included in the expression. Only the first and last names are concatenated. If the <code>MiddleName<\/code> value is an initial, a period is added to the initial and concatenated with the first and last names. Otherwise, all three names are concatenated, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now let&#8217;s modify the <code>CASE<\/code> expression by removing the <code>ELSE<\/code> clause:<\/p>\n<pre wp-pre-tag-18=\"\"><\/pre>\n<p>This time a <code>NULL<\/code> is returned if the <code>MiddleName<\/code> column has a value that is more than an initial, as the following table shows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly, you would not want to delete the <code>ELSE<\/code> clause in this instance. You could, however, add a <code>WHEN...THEN<\/code> condition that replaces the <code>ELSE<\/code> clause, in which case, you&#8217;re still returning <code>NULL<\/code> of none of the conditions are met, which might be fine under some circumstances. Consider the following example:<\/p>\n<pre wp-pre-tag-19=\"\"><\/pre>\n<p>If the <code>SalesQuota<\/code> value is great than 250,000, a <code>true<\/code> is returned. If the value is less than or equal to 250,000, we get a <code>false<\/code>. However, if the column is <code>NULL<\/code>, neither condition can be true, so the <code>CASE<\/code> expression returns a <code>NULL<\/code>, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>HigherQuota<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Stephen Jiang<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Syed Abbas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Amy Alberts<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can choose to include an <code>ELSE<\/code> clause and then provide a default value, such as <code>N\/A<\/code> (or even <code>NULL<\/code>), whether or not you think it&#8217;s necessary. In fact, many developers think you should never omit the <code>ELSE<\/code> clause, even if you believe it is impossible for one of the <code>WHEN...THEN<\/code> conditions not to be met. Someone might make a change to the database that affects the statement or your thinking about possible outcomes might not be correct.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"seventh\">&#8220;Can you shed light on the order in which the database engine processes the components in an expression?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>The way in which an expression&#8217;s elements are processed depends on several factors: their listed order, whether any subset of elements is enclosed in parentheses, and what operators are used to connect the various elements. Parentheses group elements together to ensure that those elements are processed as a unit before being incorporated into the rest of the expression. In addition, the expression as a whole adheres to the concept of <i>operator precedence,<\/i> in which the operator types determine the order in which the expression is evaluated. For example, the multiply and division arithmetic operators take precedence over the greater than (<code>&gt;<\/code>) and lesser than (<code>&lt;<\/code>) comparison operations, which take precedence over logical operators such as <code>AND<\/code> or <code>ANY<\/code>. (You can find details about operator precedence in the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190276.aspx\">Operator Precedence<\/a>.&#8221;)<\/p>\n<p>Let&#8217;s start with a few simple arithmetic-based expressions to give you a sense of how this works. The following example declares several <code>INT<\/code> variables, assigns an expression to each one, and returns the values from all three:<\/p>\n<pre wp-pre-tag-20=\"\"><\/pre>\n<p>All we&#8217;re doing in each expression is multiplying and adding and subtracting values. However, because we use parentheses in the second and third expressions, our results come up quite different:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>num1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>@num1<\/code> expression is fairly straightforward. We multiple 3 by 2, add 5, and subtract 7, which gives us a total of 4.<\/p>\n<p>However, in the <code>@num2<\/code> expression, we enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> elements in parentheses, so they&#8217;re processed first, giving us a total of 7. The 7 is then multiplied by the first 3, giving us 21. From there, we subtract 7, giving us a total of 14.<\/p>\n<p>In the <code>@num3<\/code> expression, the parentheses enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> <code>-<\/code> <code>7<\/code> elements, which are processed before the other elements. Because this returns 0, 3 is multiplied by 0, giving us a final result of 0.<\/p>\n<p>Now let&#8217;s look at another example, which again declares three variables and applies different expressions to them:<\/p>\n<pre wp-pre-tag-21=\"\"><\/pre>\n<p>Here&#8217;s what are results look like:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>SalesDiff1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesDiff2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6500<\/p>\n<\/td>\n<td valign=\"top\">\n<p>500<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;The first expression includes no parentheses. However, the multiply operator takes precedence over the addition and subtraction operators, so the multiplication is performed first. That means we first multiply the <code>SalesQuota<\/code> value by 2 and the <code>SalesYTD<\/code> value by 2, giving us the expression <code>6000<\/code> <code>-<\/code> <code>2500<\/code> <code>+<\/code> <code>3000<\/code>, which equals 6500.<\/p>\n<p>The second expression does use parentheses. The first set of parentheses returns a value of 6000. The second set of parentheses actually contains a third set, and those are the elements evaluated first, before the outer set of elements. After they&#8217;re evaluated, that part of the expression enclosed in the second set of parentheses becomes <code>@SalesLastYear<\/code> <code> +<\/code> <code>3000<\/code>, giving us a total of 5500. That 5500 is then subtracted from the 6000, which is how we end up with 500.<\/p>\n<p>Now let&#8217;s look at an expression that incorporates character data as well as numerical data. In the following <code>SELECT<\/code> statement, our <code>WHERE<\/code> clause includes both the <code>OR<\/code> and the <code>AND<\/code> logical operators:<\/p>\n<pre wp-pre-tag-22=\"\"><\/pre>\n<p>The <code>WHERE<\/code> clause search condition contains three predicates separated by the <code>AND<\/code> operator and the <code>OR<\/code> operator. In its current state, the search condition states that the <code>ListPrice<\/code> value must be greater than 2.5 times the <code>StandardCost<\/code> value <i>and<\/i> the <code>ProductLine<\/code> value must equal <code>r<\/code> <i>or<\/i> the <code>ProductLine<\/code> value must equal <code>t<\/code>. According to the rules of operator precedence, the database engine first evaluates the <code>AND<\/code> logical operator and then the <code>OR<\/code> logical operator. Because of the placement of the operators, the first two predicates must both evaluate to true <i>or<\/i> the third predicate must evaluate to true. In other words, all returned rows must meet the first two conditions or meet the third condition. As a result, the statement returns 59 rows that qualify. The following table shows part of the results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Front Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>96.7964<\/p>\n<\/td>\n<td valign=\"top\">\n<p>218.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>108.7844<\/p>\n<\/td>\n<td valign=\"top\">\n<p>245.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-Panniers, Large<\/p>\n<\/td>\n<td valign=\"top\">\n<p>51.5625<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Touring Frame &#8211; Yellow, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>199.8519<\/p>\n<\/td>\n<td valign=\"top\">\n<p>333.42<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Suppose what we were really after was to return rows that met the first condition <i>and<\/i> either the second <i>or<\/i> third condition. In this case, we would have to modify our search condition slightly:<\/p>\n<pre wp-pre-tag-23=\"\"><\/pre>\n<p>Notice that the second and third predicates are now enclosed in parentheses. Consequently, the database engine will first evaluate them as a unit. That means for all rows returned, <code>ListPrice<\/code> must be greater than 2.5 times the value of <code>StandardCost<\/code> <i>and<\/i> <code>ProductLine<\/code> must equal <code>r<\/code> <i>or<\/i> <code>t<\/code>. The following table shows all the results now returned by the query.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.4923<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.8663<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.0373<\/p>\n<\/td>\n<td valign=\"top\">\n<p>21.49<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ML Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>9.3463<\/p>\n<\/td>\n<td valign=\"top\">\n<p>24.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12.1924<\/p>\n<\/td>\n<td valign=\"top\">\n<p>32.60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.8423<\/p>\n<\/td>\n<td valign=\"top\">\n<p>28.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Notice that we now have only nine rows and that each row meets both conditions for the <code>ListPrice<\/code> and <code>ProductLine<\/code> columns. By applying the parentheses, we have better controlled how the rules of operator precedence are applied.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eighth\">&#8220;I get confused on how to treat constants in my expressions. Is there a trick in using them?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>A constant is a literal value that is either part of an expression or is the entire expression. The way in which you treat constants depends on the type of data. As a general rule, you enclose character data in single quotes, but leave numerical data without quotes, although it&#8217;s not quite as straightforward as this. Before we go into the oddities, though, let&#8217;s look at a few examples of character constants. The following T-SQL declares several character variables and then retrieves their values:<\/p>\n<pre wp-pre-tag-24=\"\"><\/pre>\n<p>By default, when you&#8217;re working with character string constants, you enclose them in singe quotes. If you&#8217;re working with Unicode strings, you precede the opening quote with an uppercase N. Anything enclosed in the quotes is considered part of that string, even if the characters are numbers, operators, or other types of symbols, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>some value<\/p>\n<\/td>\n<td valign=\"top\">&nbsp;<\/td>\n<td valign=\"top\">\n<p>some value + 1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12345<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;In SQL Server, we can instead enclose character strings in double quotes by setting the <code>QUOTED_IDENTIFIER<\/code> option to <code> OFF<\/code>, as shown in the following example:<\/p>\n<pre wp-pre-tag-25=\"\"><\/pre>\n<p>When the <code>QUOTED_IDENTIFIER<\/code> option is set to <code>OFF<\/code>, we can use double quotes just like single quotes, as is the case with the <code>@var1<\/code> declaration. The advantage of using double quotes is that you can pass in a special character, such as an apostrophe, without having to escape it by adding a second apostrophe, as we had to do for <code>@var3<\/code>. The <code>SELECT<\/code> statement now returns the results shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>its value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Be aware, however, that the <code>QUOTED_IDENTIFIER<\/code> option has other implications besides being able to use double quotes for string constants. For example, the option affects the way identifiers are handled in T-SQL. In addition, the SQL Server Native Client Provider and ODBC driver set this option to <code>ON<\/code>, which can impact your code if you had set the option to <code>OFF<\/code>. For these reasons you&#8217;re usually better off leaving the option set to <code>ON<\/code>.<\/p>\n<p>As previously mentioned, you typically don&#8217;t enclose numerical constants in quotes, even if they include currency symbols or scientific notation. For example, the following T-SQL declares four types of numerical variables, sets their values, and retrieves those values:<\/p>\n<pre wp-pre-tag-26=\"\"><\/pre>\n<p>If we had enclosed the constants in quotes, the database engine would have interpreted the values as character data and have tried to convert it to the type of the applicable variable. For the first three variables, an implicit conversion would have worked fine, but it would mean unnecessary work on the part of the database engine. For the last variable, the engine would have returned the following error:<\/p>\n<pre wp-pre-tag-27=\"\"><\/pre>\n<p>When we specify a numerical constant without quotes, the database engine assumes a numerical type consistent with the value. For example, the value <code>147<\/code> is assumed to be an <code>INT<\/code>, so no implicit conversion is necessary. The following table shows the results returned by the <code> SELECT<\/code> statement.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>147<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.5800<\/p>\n<\/td>\n<td valign=\"top\">\n<p>68.24<\/p>\n<\/td>\n<td valign=\"top\">\n<p>87.9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Even with numeric constants, an implicit conversion might be called for. In the following <code>SELECT<\/code> statement, the constant <code>1000<\/code> is compared the value in the <code>ListPrice<\/code> column, which is configured with the <code>MONEY<\/code> data type:<\/p>\n<pre wp-pre-tag-28=\"\"><\/pre>\n<p>The <code>1000<\/code> is treated as an <code>INT<\/code> value, so it must be implicitly converted to the <code>MONEY<\/code> type. But that&#8217;s not all what&#8217;s going on in the <code>WHERE<\/code> clause. Notice that it also includes a string constant (<code>blue<\/code>). The constant is compared to the <code>Color<\/code> column, which is configured with the <code>NVARCHAR<\/code> data type. This means that the database engine must convert the <code>VARCHAR<\/code> string to the <code>NVARCHAR<\/code> type, which gives us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Like string constants, date and time values are also enclosed in single quotes, even though the values are stored as integers, as you can see in the following example:<\/p>\n<pre wp-pre-tag-29=\"\"><\/pre>\n<p>The date and time data types will accept various formats when passing in a constant, as long as the value is enclosed in quotes. The following table shows the results returned by the <code>SELECT<\/code> statement:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2014-08-01 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-28 16:24:27.293<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-30<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14:05:55.0010000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;You should also be aware of how to treat constants when working with some of the other data types. For example, a <code>VARBINARY<\/code> value takes an <code>0x<\/code> prefix but is not enclosed in quotes. The same goes for a <code>BIT<\/code> value. However, a <code>UNIQUEIDENTIFIER<\/code> value is enclosed in quotes, as shown in the following T-SQL:<\/p>\n<pre wp-pre-tag-30=\"\"><\/pre>\n<p>&nbsp;Our <code>SELECT<\/code> statement now returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>0x14AD<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CC421A37-E462-4AE0-8451-38F837FC5A1A<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Again, we generally enclose string values in quotes and leave the quotes off for numerical values, but as you&#8217;ve seen, some areas can be somewhat gray. If you get confused, check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179899.aspx\">Constants<\/a>.&#8221;<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ninth\">&#8220;Are there any advantages to using a COALESCE expression rather than a CASE expression when checking for the first non-NULL value in a list of values?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>A <code>COALESCE<\/code> expression is a syntactic shortcut for a <code>CASE<\/code> expression. That means, when the query optimizer gets ahold of a <code>COALESCE<\/code> expression, it rewrites it as a <code>CASE<\/code> one, so in that sense, they&#8217;re one in the same. Let&#8217;s look at a couple of examples. In the first one, we use <code>COALESCE<\/code> to return the first column that is not a <code>NULL<\/code> value:<\/p>\n<pre wp-pre-tag-31=\"\"><\/pre>\n<p>For this particular product, the <code>Size<\/code> column and <code>SizeUnitMeasureCode<\/code> column each contains a <code>NULL<\/code> value, but the <code>WeightUnit<\/code> column contains an actual value, <code>G<\/code>, so the <code>COALESCE<\/code> expression returns that value, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Size<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SizeUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>WeightUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FirstNotNull<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G&nbsp;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G&nbsp;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Now let&#8217;s look at what happens when we change the <code> COALESCE<\/code> expression to a <code>CASE<\/code> expression:<\/p>\n<pre wp-pre-tag-32=\"\"><\/pre>\n<p>The <code>SELECT<\/code> statement returns the same results as the previous example. That said, the <code>COALESCE<\/code> expression is a lot simpler, although the logic of the <code>CASE<\/code> statement is easier to understand. In some cases, you might find that it&#8217;s better to use <code>CASE<\/code> so your intent is clear to other developers.<\/p>\n<p>One other consideration with <code>COALESCE<\/code>. Developers sometimes run into problems with <code>COALESCE<\/code> expressions because of data type issues. The value returned by <code>COALESCE<\/code> is based on the data type of the input value with the highest precedence, which might or might not be the returned value. If an implicit conversion is required, and such a conversion is not permitted, <code>COALESCE<\/code> will return an error. See the article &#8220;<a href=\"https:\/\/www.simple-talk.com\/sql\/t-sql-programming\/questions-about-sql-server-data-types-you-were-too-shy-to-ask\/\">Questions about SQL Server Data Types You were Too Shy to Ask<\/a>&#8221; for more details about this particular issue.<\/p>\n<p>All that said, figuring out the logic behind <code>COALESCE<\/code> is not too difficult, so choosing between <code>CASE<\/code> and <code>COALESCE<\/code> often comes down to nothing more than personal preference.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"tenth\">&#8220;I&#8217;m working on a query that uses the NOT IN operator in the WHERE clause. The operator checks values in a list returned by a subquery. When the returned list includes a NULL value, the query returns an empty resultset, even though I know I should be seeing results. Any idea what might be happening?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Using <code>NOT<\/code> <code>IN<\/code> can be tricky, especially when <code>NULL<\/code> values are involved. Case in point. The following <code>SELECT<\/code> statement includes a <code>WHERE<\/code> clause that checks for specific colors:<\/p>\n<pre wp-pre-tag-33=\"\"><\/pre>\n<p>Basically, what we&#8217;re doing is returning all products that are saleable items (<code>FinishedGoodsFlag<\/code> <code>=<\/code> <code>1<\/code>) and whose color is not one of the colors of the non-saleable products. Unfortunately, when we run this query we receive an empty resultset.<\/p>\n<p>If you&#8217;re familiar with the data, you might be surprised by these results. After all, the table contains 295 rows of saleable products. Either there are no colors unique to saleable items, or something is wrong.<\/p>\n<p>We can investigate the problem by first running the subquery in the example above separate from the outer query:<\/p>\n<pre wp-pre-tag-34=\"\"><\/pre>\n<p>The following table shows the subquery&#8217;s results, which includes three distinct items:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, we have black and silver non-saleable items, along with those items for which no color has been assigned (the <code>NULL<\/code> value). Now let&#8217;s modify the subquery to instead retrieve the colors of the orderable products:<\/p>\n<pre wp-pre-tag-35=\"\"><\/pre>\n<p>This time around, we again receive the same three values, along with another seven colors, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Grey<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver\/Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Yellow<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly something is wrong. Rows that contain those seven additional colors should have been included in our original query results. The problem, it turns out, is the <code>NULL<\/code> value. Let&#8217;s recast the logic of the <code>WHERE<\/code> clause into several separate predicates, connected by the <code>AND<\/code> logical operator:<\/p>\n<pre wp-pre-tag-36=\"\"><\/pre>\n<p>Essentially, our <code>NOT<\/code> <code>IN<\/code> operator indicates that the value in the <code>Color<\/code> column should not be <code>Black<\/code> <i>and<\/i> should not be <code>Silver<\/code> <i>and<\/i> should not be <code> NULL<\/code>, in addition to being a saleable product. The problem is with the equation <code>Color<\/code> <code>&lt;&gt;<\/code> <code>NULL<\/code>. For a row to be returned, all conditions must evaluate to true; however, a <code>NULL<\/code> cannot evaluate to true or false, which means the condition as a whole will never evaluate to true and no rows will ever be returned. If we were to run this statement, we would once again receive an empty resultset.<\/p>\n<p>One way around this predicament is to add a second predicate to the <code>WHERE<\/code> clause in our subquery:<\/p>\n<pre wp-pre-tag-37=\"\"><\/pre>\n<p>Now our subquery returns only the values <code>Black<\/code> and <code>Silver<\/code>, which means our outer query should return all rows except those whose <code>Color<\/code> value is one of the two colors. The following table shows part of the results, although the query actually returns 120 rows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Blue<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AWC Logo Cap<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, S<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, XL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you were to run this query, you would see the <code>Color<\/code> values <code>black<\/code> and <code>silver<\/code> are not included. However, what&#8217;s odd about this is that rows with <code>NULL<\/code> for a color are not included either, even though <code>NULL<\/code> is no longer being returned by the subquery. This might be fine in some cases, but in others you might want to see those values. Before we start looking for a workaround, however, another consideration is performance. Many SQL Server folks recommend using an <code>EXISTS<\/code> operator rather than an <code>IN<\/code> operator anyway. So let&#8217;s rewrite the last statement using <code>NOT<\/code> <code>EXISTS<\/code>:<\/p>\n<pre wp-pre-tag-38=\"\"><\/pre>\n<p>Our <code>WHERE<\/code> clause now specifies that we include only saleable products, that they be a known color, and that the color not exist within the list of non-orderable colors. As a result, the <code>SELECT<\/code> statement returns the same results as the preceding one, 120 rows. And if we want to include the saleable products with a <code>Color<\/code> value of <code>NULL<\/code>, we can simply remove the <code>IS<\/code> <code>NOT<\/code> <code>NULL<\/code> expression:<\/p>\n<pre wp-pre-tag-39=\"\"><\/pre>\n<p>Our statement now returns 170 rows, which include those rows with a <code>Color<\/code> value of <code>NULL<\/code>. In theory, this statement should perform better than the ones using <code>NOT<\/code> <code>NULL<\/code>, but you might want to test that part out yourself to be sure. Just know that using <code>NOT<\/code> <code>IN<\/code> with <code>NULL<\/code> values can return unexpected results, so be prepared.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eleventh\">&nbsp;&#8220;What the heck is a &#8216;modulo&#8217;?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Remember when you first learned long division? You had the dividend, the divisor, and the remainder. The dividend is the number you start with. The divisor is the number you divide into the dividend. The remainder is what&#8217;s left. Modulo is a mathematical operator that returns the remainder.<\/p>\n<p>Here&#8217;s how it works. In the follow example, <code>@var1<\/code> serves as our dividend, and <code>@var2<\/code> serves as our divisor:<\/p>\n<pre wp-pre-tag-40=\"\"><\/pre>\n<p>The first expression in our <code>SELECT<\/code> clause uses the division operator (<code>\/<\/code>) to divide <code>@var1<\/code> by <code>@var2<\/code>. The second expression uses the modulo operator (<code>%<\/code>) to calculate the remainder that&#8217;s produced when you divide these two numbers. The following table shows the results.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>No surprise here. When you divide 32 by 8, you get 4, with a remainder of 0. But now let&#8217;s produce a remainder other than 0:<\/p>\n<pre wp-pre-tag-41=\"\"><\/pre>\n<p>This time around, we simply set <code>@var1<\/code> to equal 36, rather than 32. Now our results show a remainder of 4.<\/p>\n<p>That&#8217;s all there is to using the modulo operator. And you can use it with other numeric types:<\/p>\n<pre wp-pre-tag-42=\"\"><\/pre>\n<p>In this case, we&#8217;re dividing 55.566 by 5 to come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>11.111200000000000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0.5560<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, the expression that contains the modulo operator returns a value of <code>0.5560<\/code>. As for the expression that contains the division operator, the operator&#8217;s returned value is based on the argument type with the highest precedence, which in this case is <code>DECIMAL<\/code>.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"twelveth\">&#8220;Are all WHEN and ELSE clauses in a CASE expression evaluated if the first WHEN clause evaluates to TRUE?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>According to the SQL Server documentation, a <code>CASE<\/code> expression evaluates each condition sequentially and stops with the first condition that evaluates to true. In other words, the expression will short circuit when a condition is met, saving the database engine from having to perform unnecessary processing. This is a good thing. The less work the engine has to perform, the better.<\/p>\n<p>To get a sense of how this works, let&#8217;s look at an example. The following <code>SELECT<\/code> statement includes a <code>CASE<\/code> expression that sets up three possible conditions when concatenating the name-related columns in the <code>vSalesPerson<\/code> view:<\/p>\n<pre wp-pre-tag-43=\"\"><\/pre>\n<p>The first <code>CASE<\/code> condition checks whether the <code>MiddleName<\/code> value is <code>NULL<\/code>. If it is, the <code>FirstName<\/code> and <code>LastName<\/code> columns are concatenated and the expression stops running. If the column contains a value other than <code>NULL<\/code>, the expression moves on to the next condition, which checks the length of the <code>MiddleName<\/code> value. If it equals <code>1<\/code>, the three columns are concatenated and a period is added to the middle initial. The expression will then stop at that point. Otherwise, the <code>ELSE<\/code> condition is applied and all three columns are concatenated as is. The database engine repeats the process for each row and returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>CASE<\/code> expression&#8217;s ability to short circuit can help to avoid unnecessary processing when it can be substituted for other statement elements. For example, suppose we define the following <code>SELECT<\/code> statement:<\/p>\n<pre wp-pre-tag-44=\"\"><\/pre>\n<p>Nothing flashy here. For each row, the database engine tests for the three specified conditions and determines whether that row is returned. Consequently, the resultset includes any saleable product that is either black or red, which means 127 rows evaluate to true. Part of those results are shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 52<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 56<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Suppose we want to reduce some of the work that the database engine has to perform. We can instead use a <code>CASE<\/code> expression in the <code>WHERE<\/code> clause, as shown in the following <code>SELECT<\/code> statement:<\/p>\n<pre wp-pre-tag-45=\"\"><\/pre>\n<p>If the <code>Color<\/code> value equals <code>black<\/code>, the condition evaluates to true and the expression stops running, thus avoiding any unnecessary processing. Yet the statement returns the same results as the preceding example.<\/p>\n<p>Under the right circumstances, using a <code>CASE<\/code> expression in this way can be a handy way to reduce your workloads. However, a <code>CASE<\/code> expression might not always short circuit in the way you hope. For example, suppose your T-SQL includes logic is similar to that shown in the following example:<\/p>\n<pre wp-pre-tag-46=\"\"><\/pre>\n<p>All we&#8217;re doing is declaring an <code>INT<\/code> variable, setting its value to <code>0<\/code>, and using a <code>CASE<\/code> expression to retrieve a specific value. Based on our understanding of the <code>CASE<\/code> expression&#8217;s ability to short circuit, we would expect the expression&#8217;s first condition to evaluate to true and the expression to then stop. However, what we get from our <code>SELECT<\/code> statement is the following error:<\/p>\n<pre wp-pre-tag-47=\"\"><\/pre>\n<p>The error shows that the <code>ELSE<\/code> condition did in fact run, even though it shouldn&#8217;t have. It turns out that under certain circumstances, a <code>CASE<\/code> expression doesn&#8217;t behave like it&#8217;s supposed to behave. The problem appears to be related to aggregate functions (at least in part), so be aware that in some cases a <code>CASE<\/code> expression might not deliver the benefits you&#8217;re hoping for and you might have to come up with a workaround to make the <code>CASE<\/code> expression work the way you want it to.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"thirteenth\">&#8220;What&#8217;s the difference between the two &#8216;not equal to&#8217; operators (&lt;&gt; and !=)?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>In T-SQL, the two comparisons operators are syntactically equivalent. They each perform a Boolean comparison between two expressions. If those expressions return non-<code>NULL<\/code>, unequal values, the comparison evaluates to true, otherwise, it evaluates to false. However, if either value is <code> NULL<\/code>, the evaluation returns a <code>NULL<\/code>. Here&#8217;s a sample of the two operators in action:<\/p>\n<pre wp-pre-tag-48=\"\"><\/pre>\n<p>&nbsp;The variable is set to a value of <code> 1<\/code>, and in each <code>SELECT<\/code> statement, that variable is compared to the constant <code>2<\/code>. As a result, each <code>SELECT<\/code> statement returns the value <code>not<\/code> <code>equal<\/code> because each comparison evaluates to true. Even in terms of performance, there doesn&#8217;t appear to be much difference between the two.<\/p>\n<p>However, there is one difference you should be aware of. The <code>&lt;&gt;<\/code> operator conforms to ANSI standards; the <code>!=<\/code> operator does not, despite that fact that a number of relational database systems support both operators. That said, even if there is only the slightest chance that you might port your script to another system, you&#8217;re better off sticking with the <code> &lt;&gt;<\/code> operator.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fourteenth\">&#8220;I&#8217;ve come across operators such as += in variable assignment SET statements. What do they mean?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>The add-equals operator (<code>+=<\/code>) is a compound operator, one of several types of compound operators supported in SQL Server. A compound operator takes the original value and in some way amends it, rather than replacing it. For example, the following T-SQL uses the add-equals operator to update the value of the <code>@var1<\/code> variable:<\/p>\n<pre wp-pre-tag-49=\"\"><\/pre>\n<p>We start by declaring <code>@var1<\/code> and setting its value to <code>2<\/code>. We then use a <code>SET<\/code> statement to add a value of <code>1<\/code> to the original value. The <code>SELECT<\/code> statement returns the new value, which is <code>3<\/code>.<\/p>\n<p>The add-equals operator makes it possible to add a value without having to specify the original value. Without the add-equals operator we would have to rewrite our <code>SET<\/code> statement as follows:<\/p>\n<pre wp-pre-tag-50=\"\"><\/pre>\n<p>The add-equals operator merely provides the shorthand necessary to avoid having to explicitly specify the original value, yet the <code>SELECT<\/code> statement still returns a value of <code>3<\/code>. Plus, SQL Server supports other compound operators as well. For example, the following T-SQL subtracts <code>1<\/code> from the original value:<\/p>\n<pre wp-pre-tag-51=\"\"><\/pre>\n<p>In this case, we&#8217;re using the subtract-equals (<code>-=<\/code>) operator in our <code>SELECT<\/code> statement, rather than the add-equals operator. The subtract-equals operator works the same way as the add-equals operator, except that the second value is subtracted rather than added. As a result, our <code>SELECT<\/code> statement now returns a value of <code>1<\/code>.<\/p>\n<p>Another example of a compound operator is the multiply-equals operator, which is shown in the following example:<\/p>\n<pre wp-pre-tag-52=\"\"><\/pre>\n<p>In this case, the <code>SET<\/code> statement uses the multiply-equals operator to multiple the variable value <code>2<\/code> by the constant value <code>3<\/code>, giving us a total of <code>6<\/code>. And compound operators are not limited to mathematical expressions. For example, the following T-SQL includes a <code>SET<\/code> statement that uses the add-equals operator to concatenate values:<\/p>\n<pre wp-pre-tag-53=\"\"><\/pre>\n<p>In this example, the <code>SET<\/code> statement adds the string value <code>and<\/code> <code>then<\/code> <code>some<\/code> (including the opening space) to the variable value <code>some<\/code> <code>value<\/code>, giving us the value <code>some<\/code> <code>value<\/code> <code>and<\/code> <code>then<\/code> <code>some<\/code>. There are other compound operators as well, such as divide-equals (<code>\/=<\/code>) and modulo-equals (<code>%=<\/code>). Check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc645922.aspx\">Compound Operators<\/a>&#8221; for more information.<\/p>\n<\/div>\n\n\n\n<p>+ CONVERT(NVARCHAR(20), CAST(@var2 AS MONEY), 1) AS amount2, &nbsp; &#8216;<\/p>\n\n\n\n<p>Each expression in the <code>SELECT<\/code> list references one of the variables as its source data and is made up of multiple components. What provides the final format, however, is the <code>CONVERT<\/code> function. The function requires that the source data be either the <code>MONEY<\/code> or <code>SMALLMONEY<\/code> type. When you call the function, you specify a second argument along with the source value. That argument determines the format of the data. In this case, we specify <code>1<\/code> so the outputted data includes commas every three digits to the left of the decimal point. We also concatenate the value with a dollar sign to give it that final touch, as shown in the following results:<\/p>\n\n\n\n<figure class=\"wp-block-table MsoTableGrid\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p><b>amount1<\/b><\/p>\n<\/td><td>\n<p><b>amount2<\/b><\/p>\n<\/td><td>\n<p><b>amount3<\/b><\/p>\n<\/td><\/tr><tr><td>\n<p>$555,555,555.56<\/p>\n<\/td><td>\n<p>$555,555,555.56<\/p>\n<\/td><td>\n<p>$555,555,555.56<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&nbsp;Overall, this isn&#8217;t too painful a process, unless you&#8217;re working with currencies that follow a different format, such as the Swiss franc, which takes apostrophes rather than commas. In such cases, you might have to add yet another function, such as <code>REPLACE<\/code>, to substitute the commas with apostrophes.<\/p>\n\n\n\n<p>Fortunately, since the release of SQL Server 2012, you&#8217;ve been able to instead use the <code>FORMAT<\/code> function to output your numbers to a currency format. The <code>FORMAT<\/code> function returns a value in a specified format, based on a specified culture. To demonstrate how this works, let&#8217;s first recast our preceding expressions into ones that use the <code>FORMAT<\/code> function to configure the data as US dollar amounts:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>Notice that we must first cast the character data (<code>@var1<\/code>) to the <code>MONEY<\/code> data type, but we can use the <code>DECIMAL<\/code> variable (<code>@var2<\/code>) as is. The second <code>FORMAT<\/code> argument (<code>c<\/code>) specifies that we&#8217;re formatting currency, and the third argument specifies the US symbol (<code>en-us<\/code>) for the cultural context. The <code>SELECT<\/code> statement returns the same results as the previous <code>SELECT<\/code> statement, with all values returned as the <code>NVARCHAR<\/code> type.<\/p>\n\n\n\n<p>However, with the <code>FORMAT<\/code> function, we can specify other cultures. For example, the following example uses the German symbol (<code>de-de<\/code>) to provide the cultural context:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>Now our results are specific to how the euro is represented in Germany, as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table MsoTableGrid\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p><b>amount1<\/b><\/p>\n<\/td><td>\n<p><b>amount2<\/b><\/p>\n<\/td><td>\n<p><b>amount3<\/b><\/p>\n<\/td><\/tr><tr><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&nbsp;Certainly, the <code>FORMAT<\/code> function is a handy tool if you want your results to be displayed in a specific currency. But just to reiterate, such formatting is generally handled by the calling application, with the source data retrieved in its raw state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"third\">&#8220;I see the plus sign (+) used in a variety of ways in T-SQL script, but I can&#8217;t always make sense of how SQL Server arrives at the results it does. Is there a &#8220;right way&#8221; to use plus signs?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>SQL Server is a tricky devil. As you&#8217;ve discovered, plus signs have all sorts of meaning in T-SQL. You can use them to concatenate string values, add numerical values together, or indicate that a numerical value is a positive number, rather than negative. The following <code>SELECT<\/code> statement demonstrates how to both concatenate and add values:<\/p>\n<pre wp-pre-tag-4=\"\"><\/pre>\n<p>For the <code>FullName<\/code> column, we&#8217;ve used the plus sign to concatenate the <code>FirstName<\/code> and <code> LastName<\/code> columns with a space in between. For the <code>TwoYearSales<\/code> column, we&#8217;ve used the plus sign to add the <code>SalesLastYear<\/code> and <code> SalesYTD<\/code> columns together. In both cases, the database engine knows when to concatenate the values or when to add them together, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesLastYear<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesYTD<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>TwoYearSales<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,750,406.48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,763,178.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,513,584.66<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,439,156.03<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,251,368.55<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,690,524.58<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,997,186.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,189,418.37<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,186,604.57<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Garrett Vargas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,620,276.90<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,453,719.47<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,073,996.36<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,849,640.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,315,185.61<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,164,826.55<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,927,059.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,352,577.13<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,279,636.31<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,073,506.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,458,535.62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,532,041.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jos\u00e9 Saraiva<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,038,234.65<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,604,540.72<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,642,775.37<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,371,635.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,573,012.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,944,648.25<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$0.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Lynn Tsoflias<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,278,548.98<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,421,810.92<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,700,359.90<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Rachel Valdez<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,307,949.79<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,827,066.71<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,135,016.50<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jae Pak<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,635,823.40<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,116,871.23<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,752,694.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Ranjit Varkey Chudukatil<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,396,539.76<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,121,616.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,518,156.08<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There&#8217;s no real mystery here. When we use the plus sign with only character data, the database engine concatenates the values. In this case, the plus sign is considered a string concatenation operator. When we use the plus sign with numerical values, the database engine adds the values together. Under these circumstances, the plus sign is considered an addition operator.<\/p>\n<p>Not surprisingly, when we mix and match the types of data, our results are less predictable. Take a look at the following example:<\/p>\n<pre wp-pre-tag-5=\"\"><\/pre>\n<p>As you can see, we&#8217;ve declared seven variables, each defined with a different type. We then use the plus sign to combine the variables in different ways, giving us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>TestVarCX<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1299.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1001.0001<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2015-05-21 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2017-04-19 23:45:36.000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As we would expect, when we pair <code>@var1<\/code> with <code>@var2<\/code>, both character types, we end up with a concatenated string. When we pair <code>@var3<\/code> to <code>@var6<\/code>, both numerical types, the values are added together, even though <code>@var3<\/code> and <code>@var6<\/code> are different types.<\/p>\n<p>Now we get to our third pairing: <code>@var4<\/code> <code>+<\/code> <code>@var5<\/code>. This time around, we&#8217;re adding a <code>DECIMAL<\/code> value to <code>BIT<\/code> value. However, the database engine still treats both variables as numerical values and adds them together, even though SQL Server documentation suggests that you can&#8217;t do this with the <code>BIT<\/code> type. (And really, why would you?)<\/p>\n<p>The next item in the select list, <code>@var7<\/code> <code>+<\/code> <code>@var3<\/code>, pairs a <code>DATETIME<\/code> value with a <code>SMALLINT<\/code> value. This time, we get a date that is 300 days later than the original date. When your expression includes the plus sign, the database engine defaults to the data type with the highest precedence, in this case, <code> DATETIME<\/code>. Based on the type, the engine determines whether to use the plus sign to concatenate values or add them together. If the data type with the highest precedence is a numeric data type, the engine attempts to add the values. In this case, the engine adds the <code>SMALLINT<\/code> value to the <code>DATETIME<\/code> value as the specified number of days. (A <code>DATETIME<\/code> value is actually stored as two integers.)<\/p>\n<p>Finally, we pair a <code>MONEY<\/code> date type with the <code>DATETIME<\/code> date type (<code>@var6<\/code> <code>+<\/code> <code>@var7<\/code>). Once again, <code>DATETIME<\/code> takes precedence over <code>MONEY<\/code>, so the database engine adds 999.99 days to our original date to come up with a date in 2017.<\/p>\n<p>We can verify how data type precedence works with plus signs by using the <code>SQL_VARIANT_PROPERTY<\/code> function to retrieve the data type of the expressions that pair the different variables:<\/p>\n<pre wp-pre-tag-6=\"\"><\/pre>\n<p>The <code>SELECT<\/code> statement returns the returned data type for each expression, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>money<\/p>\n<\/td>\n<td valign=\"top\">\n<p>decimal<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;In each case, the expression returns the type with the highest precedence and determines how the plus sign will be used.<\/p>\n<p>So far in our examples, the database engine has been able to add or concatenate values without a problem because the engine could implicitly convert the data type with the lowest precedence to the type with the highest. But this isn&#8217;t always the case:<\/p>\n<pre wp-pre-tag-7=\"\"><\/pre>\n<p>This time around, we&#8217;re using the plus sign to pair a <code>NVARCHAR<\/code> value with a <code>SMALLINT<\/code> value. Because <code>SMALLINT<\/code> has precedence over <code>NVARCHAR<\/code>, the database engine attempts to convert the value <code>TestVar<\/code> to the <code>SMALLINT<\/code> type, which of course is not possible. Consequently, the database engine returns the following error message:<\/p>\n<pre wp-pre-tag-8=\"\"><\/pre>\n<p>The only way we can pair a numerical value with a string value is to concatenate them. In order to do so, we must explicitly convert the <code>SMALLINT<\/code> value to a character data type:<\/p>\n<pre wp-pre-tag-9=\"\"><\/pre>\n<p>Now our <code>SELECT<\/code> statement returns the value <code>TestVar300<\/code>.<\/p>\n<p>There&#8217;s one other use of the plus sign that you should be aware of-as a unary plus operator. SQL Server supports two types of unary operators, plus and minus, which are used to designate whether a numeric value is positive or negative:<\/p>\n<pre wp-pre-tag-10=\"\"><\/pre>\n<p>All we&#8217;ve done here is to designate our two literal values as positive and negative numbers. The <code>SELECT<\/code> statement returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>123.45<\/p>\n<\/td>\n<td valign=\"top\">\n<p>-123.45<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Be aware, however, that you cannot use the positive unary operator to convert a negative number to a positive number. For that, you need to use the <code>ABS<\/code> mathematical function.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fourth\">&#8220;The NULLIF expression makes no sense. I would think you&#8217;d use it to somehow ferret out a NULL value or to test a condition for a NULL value. But it appears to merely return a NULL if two values are equal. Is that all it&#8217;s doing?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>That&#8217;s part of what it&#8217;s doing. When a <code>NULLIF<\/code> expression compares two values, it returns the first value if the two values are not equal. If they are equal, the expression returns a <code>NULL<\/code> value of the same data type as the first specified value. Here&#8217;s what a <code>NULLIF<\/code> expression looks like in action:<\/p>\n<pre wp-pre-tag-11=\"\"><\/pre>\n<p>In this case, the <code>NULLIF<\/code> expression returns <code>valueA<\/code> because the two values are not equal. If they were equal, the returned value would be <code>NULL<\/code>. In addition, the returned value would be <code>NULL<\/code> if the first value were <code>NULL<\/code>.<\/p>\n<p>When you know how a <code>NULLIF<\/code> expression works, it doesn&#8217;t seem so bad, although its use still has a tendency to cause confusion. Luckily, we can achieve the same results by using a <code>CASE<\/code> statement:<\/p>\n<pre wp-pre-tag-12=\"\"><\/pre>\n<p>The advantage of the <code>CASE<\/code> statement is that it&#8217;s a lot easier to understand. Sure, <code>NULLIF<\/code> provides simpler syntax, but if it creates confusion, what&#8217;s the point?<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fifth\">&#8220;Can you use expressions in data manipulation language (DML) statements?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>You can use expressions wherever T-SQL syntax permits them, including DML statements. Take a look at part of the syntax for the <code>UPDATE<\/code> statement&#8217;s <code>SET<\/code> clause:<\/p>\n<pre wp-pre-tag-13=\"\"><\/pre>\n<p>&nbsp;According to SQL Server Books Online, the expression placeholder can be a &#8220;variable, literal value, expression, or a subselect statement (enclosed with parentheses) that returns a single value.&#8221; This pretty much defines the term <i>expression<\/i> as we understand it. Note, however, that any expression you define must conform to the rules associated with the particular T-SQL statement in which the expression is defined. For example, an <code>UPDATE<\/code> statement&#8217;s <code>UPDATE<\/code> clause can include the <code>TOP<\/code> clause, as shown in the following syntax:<\/p>\n<pre wp-pre-tag-14=\"\"><\/pre>\n<p>&nbsp;In this case, expression &#8220;specifies the number or percent of rows that will be updated.&#8221; In other words, you cannot specify a non-numerical string value as your expression, as you can with other types of expressions. That said, it&#8217;s still considered an expression.<\/p>\n<p>So, yes, you can use an expression in a DML statement. In fact, they&#8217;re used all the time. Let&#8217;s look at an example. Suppose we create the following table in our database and insert a row of data:<\/p>\n<pre wp-pre-tag-15=\"\"><\/pre>\n<p>Now let&#8217;s use an <code>UPDATE<\/code> statement to modify that row of data:<\/p>\n<pre wp-pre-tag-16=\"\"><\/pre>\n<p>Our <code>SET<\/code> clause is updating two columns. Each <code>SET<\/code> definition includes an expression that either adds the <code>@AmtReceived<\/code> value to the target column or subtracts that value. If we run the <code>SELECT<\/code> statement after the table has been updated, we come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>InStock<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>OnOrder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>67<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you&#8217;re writing T-SQL statements, you&#8217;re writing expressions, even if you don&#8217;t realize it. That&#8217;s why it&#8217;s important to refer back to the statement&#8217;s syntax whenever you&#8217;re uncertain how to define a particular element. Expressions are supported throughout most statements. The syntax for the <code>UPDATE<\/code> statement, for example, includes the expression placeholder in 10 different places, which suggests that many of us might not be using some statements to their fullest capacity.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"sixth\">&nbsp;&#8220;The CASE expression allows me to leave out the ELSE clause. Is it that important to include it?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Yes, but&#8230;. The <code>ELSE<\/code> clause specifies the value to return if no other comparison operations (<code>WHEN...THEN<\/code>) evaluate to true. If you omit the <code>ELSE<\/code> clause, the database engine instead returns a <code>NULL<\/code> value. A <code>NULL<\/code> value might be fine, if that&#8217;s what you want. But what if it&#8217;s not? Take a look at the following example, which uses a <code>CASE<\/code> expression to determine how to return an employee&#8217;s full name:<\/p>\n<pre wp-pre-tag-17=\"\"><\/pre>\n<p>If the <code>MiddleName<\/code> column is <code>NULL<\/code>, that column is not included in the expression. Only the first and last names are concatenated. If the <code>MiddleName<\/code> value is an initial, a period is added to the initial and concatenated with the first and last names. Otherwise, all three names are concatenated, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now let&#8217;s modify the <code>CASE<\/code> expression by removing the <code>ELSE<\/code> clause:<\/p>\n<pre wp-pre-tag-18=\"\"><\/pre>\n<p>This time a <code>NULL<\/code> is returned if the <code>MiddleName<\/code> column has a value that is more than an initial, as the following table shows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly, you would not want to delete the <code>ELSE<\/code> clause in this instance. You could, however, add a <code>WHEN...THEN<\/code> condition that replaces the <code>ELSE<\/code> clause, in which case, you&#8217;re still returning <code>NULL<\/code> of none of the conditions are met, which might be fine under some circumstances. Consider the following example:<\/p>\n<pre wp-pre-tag-19=\"\"><\/pre>\n<p>If the <code>SalesQuota<\/code> value is great than 250,000, a <code>true<\/code> is returned. If the value is less than or equal to 250,000, we get a <code>false<\/code>. However, if the column is <code>NULL<\/code>, neither condition can be true, so the <code>CASE<\/code> expression returns a <code>NULL<\/code>, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>HigherQuota<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Stephen Jiang<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Syed Abbas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Amy Alberts<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can choose to include an <code>ELSE<\/code> clause and then provide a default value, such as <code>N\/A<\/code> (or even <code>NULL<\/code>), whether or not you think it&#8217;s necessary. In fact, many developers think you should never omit the <code>ELSE<\/code> clause, even if you believe it is impossible for one of the <code>WHEN...THEN<\/code> conditions not to be met. Someone might make a change to the database that affects the statement or your thinking about possible outcomes might not be correct.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"seventh\">&#8220;Can you shed light on the order in which the database engine processes the components in an expression?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>The way in which an expression&#8217;s elements are processed depends on several factors: their listed order, whether any subset of elements is enclosed in parentheses, and what operators are used to connect the various elements. Parentheses group elements together to ensure that those elements are processed as a unit before being incorporated into the rest of the expression. In addition, the expression as a whole adheres to the concept of <i>operator precedence,<\/i> in which the operator types determine the order in which the expression is evaluated. For example, the multiply and division arithmetic operators take precedence over the greater than (<code>&gt;<\/code>) and lesser than (<code>&lt;<\/code>) comparison operations, which take precedence over logical operators such as <code>AND<\/code> or <code>ANY<\/code>. (You can find details about operator precedence in the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190276.aspx\">Operator Precedence<\/a>.&#8221;)<\/p>\n<p>Let&#8217;s start with a few simple arithmetic-based expressions to give you a sense of how this works. The following example declares several <code>INT<\/code> variables, assigns an expression to each one, and returns the values from all three:<\/p>\n<pre wp-pre-tag-20=\"\"><\/pre>\n<p>All we&#8217;re doing in each expression is multiplying and adding and subtracting values. However, because we use parentheses in the second and third expressions, our results come up quite different:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>num1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>@num1<\/code> expression is fairly straightforward. We multiple 3 by 2, add 5, and subtract 7, which gives us a total of 4.<\/p>\n<p>However, in the <code>@num2<\/code> expression, we enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> elements in parentheses, so they&#8217;re processed first, giving us a total of 7. The 7 is then multiplied by the first 3, giving us 21. From there, we subtract 7, giving us a total of 14.<\/p>\n<p>In the <code>@num3<\/code> expression, the parentheses enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> <code>-<\/code> <code>7<\/code> elements, which are processed before the other elements. Because this returns 0, 3 is multiplied by 0, giving us a final result of 0.<\/p>\n<p>Now let&#8217;s look at another example, which again declares three variables and applies different expressions to them:<\/p>\n<pre wp-pre-tag-21=\"\"><\/pre>\n<p>Here&#8217;s what are results look like:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>SalesDiff1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesDiff2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6500<\/p>\n<\/td>\n<td valign=\"top\">\n<p>500<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;The first expression includes no parentheses. However, the multiply operator takes precedence over the addition and subtraction operators, so the multiplication is performed first. That means we first multiply the <code>SalesQuota<\/code> value by 2 and the <code>SalesYTD<\/code> value by 2, giving us the expression <code>6000<\/code> <code>-<\/code> <code>2500<\/code> <code>+<\/code> <code>3000<\/code>, which equals 6500.<\/p>\n<p>The second expression does use parentheses. The first set of parentheses returns a value of 6000. The second set of parentheses actually contains a third set, and those are the elements evaluated first, before the outer set of elements. After they&#8217;re evaluated, that part of the expression enclosed in the second set of parentheses becomes <code>@SalesLastYear<\/code> <code> +<\/code> <code>3000<\/code>, giving us a total of 5500. That 5500 is then subtracted from the 6000, which is how we end up with 500.<\/p>\n<p>Now let&#8217;s look at an expression that incorporates character data as well as numerical data. In the following <code>SELECT<\/code> statement, our <code>WHERE<\/code> clause includes both the <code>OR<\/code> and the <code>AND<\/code> logical operators:<\/p>\n<pre wp-pre-tag-22=\"\"><\/pre>\n<p>The <code>WHERE<\/code> clause search condition contains three predicates separated by the <code>AND<\/code> operator and the <code>OR<\/code> operator. In its current state, the search condition states that the <code>ListPrice<\/code> value must be greater than 2.5 times the <code>StandardCost<\/code> value <i>and<\/i> the <code>ProductLine<\/code> value must equal <code>r<\/code> <i>or<\/i> the <code>ProductLine<\/code> value must equal <code>t<\/code>. According to the rules of operator precedence, the database engine first evaluates the <code>AND<\/code> logical operator and then the <code>OR<\/code> logical operator. Because of the placement of the operators, the first two predicates must both evaluate to true <i>or<\/i> the third predicate must evaluate to true. In other words, all returned rows must meet the first two conditions or meet the third condition. As a result, the statement returns 59 rows that qualify. The following table shows part of the results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Front Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>96.7964<\/p>\n<\/td>\n<td valign=\"top\">\n<p>218.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>108.7844<\/p>\n<\/td>\n<td valign=\"top\">\n<p>245.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-Panniers, Large<\/p>\n<\/td>\n<td valign=\"top\">\n<p>51.5625<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Touring Frame &#8211; Yellow, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>199.8519<\/p>\n<\/td>\n<td valign=\"top\">\n<p>333.42<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Suppose what we were really after was to return rows that met the first condition <i>and<\/i> either the second <i>or<\/i> third condition. In this case, we would have to modify our search condition slightly:<\/p>\n<pre wp-pre-tag-23=\"\"><\/pre>\n<p>Notice that the second and third predicates are now enclosed in parentheses. Consequently, the database engine will first evaluate them as a unit. That means for all rows returned, <code>ListPrice<\/code> must be greater than 2.5 times the value of <code>StandardCost<\/code> <i>and<\/i> <code>ProductLine<\/code> must equal <code>r<\/code> <i>or<\/i> <code>t<\/code>. The following table shows all the results now returned by the query.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.4923<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.8663<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.0373<\/p>\n<\/td>\n<td valign=\"top\">\n<p>21.49<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ML Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>9.3463<\/p>\n<\/td>\n<td valign=\"top\">\n<p>24.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12.1924<\/p>\n<\/td>\n<td valign=\"top\">\n<p>32.60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.8423<\/p>\n<\/td>\n<td valign=\"top\">\n<p>28.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Notice that we now have only nine rows and that each row meets both conditions for the <code>ListPrice<\/code> and <code>ProductLine<\/code> columns. By applying the parentheses, we have better controlled how the rules of operator precedence are applied.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eighth\">&#8220;I get confused on how to treat constants in my expressions. Is there a trick in using them?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>A constant is a literal value that is either part of an expression or is the entire expression. The way in which you treat constants depends on the type of data. As a general rule, you enclose character data in single quotes, but leave numerical data without quotes, although it&#8217;s not quite as straightforward as this. Before we go into the oddities, though, let&#8217;s look at a few examples of character constants. The following T-SQL declares several character variables and then retrieves their values:<\/p>\n<pre wp-pre-tag-24=\"\"><\/pre>\n<p>By default, when you&#8217;re working with character string constants, you enclose them in singe quotes. If you&#8217;re working with Unicode strings, you precede the opening quote with an uppercase N. Anything enclosed in the quotes is considered part of that string, even if the characters are numbers, operators, or other types of symbols, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>some value<\/p>\n<\/td>\n<td valign=\"top\">&nbsp;<\/td>\n<td valign=\"top\">\n<p>some value + 1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12345<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;In SQL Server, we can instead enclose character strings in double quotes by setting the <code>QUOTED_IDENTIFIER<\/code> option to <code> OFF<\/code>, as shown in the following example:<\/p>\n<pre wp-pre-tag-25=\"\"><\/pre>\n<p>When the <code>QUOTED_IDENTIFIER<\/code> option is set to <code>OFF<\/code>, we can use double quotes just like single quotes, as is the case with the <code>@var1<\/code> declaration. The advantage of using double quotes is that you can pass in a special character, such as an apostrophe, without having to escape it by adding a second apostrophe, as we had to do for <code>@var3<\/code>. The <code>SELECT<\/code> statement now returns the results shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>its value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Be aware, however, that the <code>QUOTED_IDENTIFIER<\/code> option has other implications besides being able to use double quotes for string constants. For example, the option affects the way identifiers are handled in T-SQL. In addition, the SQL Server Native Client Provider and ODBC driver set this option to <code>ON<\/code>, which can impact your code if you had set the option to <code>OFF<\/code>. For these reasons you&#8217;re usually better off leaving the option set to <code>ON<\/code>.<\/p>\n<p>As previously mentioned, you typically don&#8217;t enclose numerical constants in quotes, even if they include currency symbols or scientific notation. For example, the following T-SQL declares four types of numerical variables, sets their values, and retrieves those values:<\/p>\n<pre wp-pre-tag-26=\"\"><\/pre>\n<p>If we had enclosed the constants in quotes, the database engine would have interpreted the values as character data and have tried to convert it to the type of the applicable variable. For the first three variables, an implicit conversion would have worked fine, but it would mean unnecessary work on the part of the database engine. For the last variable, the engine would have returned the following error:<\/p>\n<pre wp-pre-tag-27=\"\"><\/pre>\n<p>When we specify a numerical constant without quotes, the database engine assumes a numerical type consistent with the value. For example, the value <code>147<\/code> is assumed to be an <code>INT<\/code>, so no implicit conversion is necessary. The following table shows the results returned by the <code> SELECT<\/code> statement.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>147<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.5800<\/p>\n<\/td>\n<td valign=\"top\">\n<p>68.24<\/p>\n<\/td>\n<td valign=\"top\">\n<p>87.9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Even with numeric constants, an implicit conversion might be called for. In the following <code>SELECT<\/code> statement, the constant <code>1000<\/code> is compared the value in the <code>ListPrice<\/code> column, which is configured with the <code>MONEY<\/code> data type:<\/p>\n<pre wp-pre-tag-28=\"\"><\/pre>\n<p>The <code>1000<\/code> is treated as an <code>INT<\/code> value, so it must be implicitly converted to the <code>MONEY<\/code> type. But that&#8217;s not all what&#8217;s going on in the <code>WHERE<\/code> clause. Notice that it also includes a string constant (<code>blue<\/code>). The constant is compared to the <code>Color<\/code> column, which is configured with the <code>NVARCHAR<\/code> data type. This means that the database engine must convert the <code>VARCHAR<\/code> string to the <code>NVARCHAR<\/code> type, which gives us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Like string constants, date and time values are also enclosed in single quotes, even though the values are stored as integers, as you can see in the following example:<\/p>\n<pre wp-pre-tag-29=\"\"><\/pre>\n<p>The date and time data types will accept various formats when passing in a constant, as long as the value is enclosed in quotes. The following table shows the results returned by the <code>SELECT<\/code> statement:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2014-08-01 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-28 16:24:27.293<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-30<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14:05:55.0010000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;You should also be aware of how to treat constants when working with some of the other data types. For example, a <code>VARBINARY<\/code> value takes an <code>0x<\/code> prefix but is not enclosed in quotes. The same goes for a <code>BIT<\/code> value. However, a <code>UNIQUEIDENTIFIER<\/code> value is enclosed in quotes, as shown in the following T-SQL:<\/p>\n<pre wp-pre-tag-30=\"\"><\/pre>\n<p>&nbsp;Our <code>SELECT<\/code> statement now returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>0x14AD<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CC421A37-E462-4AE0-8451-38F837FC5A1A<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Again, we generally enclose string values in quotes and leave the quotes off for numerical values, but as you&#8217;ve seen, some areas can be somewhat gray. If you get confused, check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179899.aspx\">Constants<\/a>.&#8221;<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ninth\">&#8220;Are there any advantages to using a COALESCE expression rather than a CASE expression when checking for the first non-NULL value in a list of values?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>A <code>COALESCE<\/code> expression is a syntactic shortcut for a <code>CASE<\/code> expression. That means, when the query optimizer gets ahold of a <code>COALESCE<\/code> expression, it rewrites it as a <code>CASE<\/code> one, so in that sense, they&#8217;re one in the same. Let&#8217;s look at a couple of examples. In the first one, we use <code>COALESCE<\/code> to return the first column that is not a <code>NULL<\/code> value:<\/p>\n<pre wp-pre-tag-31=\"\"><\/pre>\n<p>For this particular product, the <code>Size<\/code> column and <code>SizeUnitMeasureCode<\/code> column each contains a <code>NULL<\/code> value, but the <code>WeightUnit<\/code> column contains an actual value, <code>G<\/code>, so the <code>COALESCE<\/code> expression returns that value, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Size<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SizeUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>WeightUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FirstNotNull<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G&nbsp;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G&nbsp;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Now let&#8217;s look at what happens when we change the <code> COALESCE<\/code> expression to a <code>CASE<\/code> expression:<\/p>\n<pre wp-pre-tag-32=\"\"><\/pre>\n<p>The <code>SELECT<\/code> statement returns the same results as the previous example. That said, the <code>COALESCE<\/code> expression is a lot simpler, although the logic of the <code>CASE<\/code> statement is easier to understand. In some cases, you might find that it&#8217;s better to use <code>CASE<\/code> so your intent is clear to other developers.<\/p>\n<p>One other consideration with <code>COALESCE<\/code>. Developers sometimes run into problems with <code>COALESCE<\/code> expressions because of data type issues. The value returned by <code>COALESCE<\/code> is based on the data type of the input value with the highest precedence, which might or might not be the returned value. If an implicit conversion is required, and such a conversion is not permitted, <code>COALESCE<\/code> will return an error. See the article &#8220;<a href=\"https:\/\/www.simple-talk.com\/sql\/t-sql-programming\/questions-about-sql-server-data-types-you-were-too-shy-to-ask\/\">Questions about SQL Server Data Types You were Too Shy to Ask<\/a>&#8221; for more details about this particular issue.<\/p>\n<p>All that said, figuring out the logic behind <code>COALESCE<\/code> is not too difficult, so choosing between <code>CASE<\/code> and <code>COALESCE<\/code> often comes down to nothing more than personal preference.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"tenth\">&#8220;I&#8217;m working on a query that uses the NOT IN operator in the WHERE clause. The operator checks values in a list returned by a subquery. When the returned list includes a NULL value, the query returns an empty resultset, even though I know I should be seeing results. Any idea what might be happening?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Using <code>NOT<\/code> <code>IN<\/code> can be tricky, especially when <code>NULL<\/code> values are involved. Case in point. The following <code>SELECT<\/code> statement includes a <code>WHERE<\/code> clause that checks for specific colors:<\/p>\n<pre wp-pre-tag-33=\"\"><\/pre>\n<p>Basically, what we&#8217;re doing is returning all products that are saleable items (<code>FinishedGoodsFlag<\/code> <code>=<\/code> <code>1<\/code>) and whose color is not one of the colors of the non-saleable products. Unfortunately, when we run this query we receive an empty resultset.<\/p>\n<p>If you&#8217;re familiar with the data, you might be surprised by these results. After all, the table contains 295 rows of saleable products. Either there are no colors unique to saleable items, or something is wrong.<\/p>\n<p>We can investigate the problem by first running the subquery in the example above separate from the outer query:<\/p>\n<pre wp-pre-tag-34=\"\"><\/pre>\n<p>The following table shows the subquery&#8217;s results, which includes three distinct items:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, we have black and silver non-saleable items, along with those items for which no color has been assigned (the <code>NULL<\/code> value). Now let&#8217;s modify the subquery to instead retrieve the colors of the orderable products:<\/p>\n<pre wp-pre-tag-35=\"\"><\/pre>\n<p>This time around, we again receive the same three values, along with another seven colors, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Grey<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver\/Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Yellow<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly something is wrong. Rows that contain those seven additional colors should have been included in our original query results. The problem, it turns out, is the <code>NULL<\/code> value. Let&#8217;s recast the logic of the <code>WHERE<\/code> clause into several separate predicates, connected by the <code>AND<\/code> logical operator:<\/p>\n<pre wp-pre-tag-36=\"\"><\/pre>\n<p>Essentially, our <code>NOT<\/code> <code>IN<\/code> operator indicates that the value in the <code>Color<\/code> column should not be <code>Black<\/code> <i>and<\/i> should not be <code>Silver<\/code> <i>and<\/i> should not be <code> NULL<\/code>, in addition to being a saleable product. The problem is with the equation <code>Color<\/code> <code>&lt;&gt;<\/code> <code>NULL<\/code>. For a row to be returned, all conditions must evaluate to true; however, a <code>NULL<\/code> cannot evaluate to true or false, which means the condition as a whole will never evaluate to true and no rows will ever be returned. If we were to run this statement, we would once again receive an empty resultset.<\/p>\n<p>One way around this predicament is to add a second predicate to the <code>WHERE<\/code> clause in our subquery:<\/p>\n<pre wp-pre-tag-37=\"\"><\/pre>\n<p>Now our subquery returns only the values <code>Black<\/code> and <code>Silver<\/code>, which means our outer query should return all rows except those whose <code>Color<\/code> value is one of the two colors. The following table shows part of the results, although the query actually returns 120 rows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Blue<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AWC Logo Cap<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, S<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, XL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you were to run this query, you would see the <code>Color<\/code> values <code>black<\/code> and <code>silver<\/code> are not included. However, what&#8217;s odd about this is that rows with <code>NULL<\/code> for a color are not included either, even though <code>NULL<\/code> is no longer being returned by the subquery. This might be fine in some cases, but in others you might want to see those values. Before we start looking for a workaround, however, another consideration is performance. Many SQL Server folks recommend using an <code>EXISTS<\/code> operator rather than an <code>IN<\/code> operator anyway. So let&#8217;s rewrite the last statement using <code>NOT<\/code> <code>EXISTS<\/code>:<\/p>\n<pre wp-pre-tag-38=\"\"><\/pre>\n<p>Our <code>WHERE<\/code> clause now specifies that we include only saleable products, that they be a known color, and that the color not exist within the list of non-orderable colors. As a result, the <code>SELECT<\/code> statement returns the same results as the preceding one, 120 rows. And if we want to include the saleable products with a <code>Color<\/code> value of <code>NULL<\/code>, we can simply remove the <code>IS<\/code> <code>NOT<\/code> <code>NULL<\/code> expression:<\/p>\n<pre wp-pre-tag-39=\"\"><\/pre>\n<p>Our statement now returns 170 rows, which include those rows with a <code>Color<\/code> value of <code>NULL<\/code>. In theory, this statement should perform better than the ones using <code>NOT<\/code> <code>NULL<\/code>, but you might want to test that part out yourself to be sure. Just know that using <code>NOT<\/code> <code>IN<\/code> with <code>NULL<\/code> values can return unexpected results, so be prepared.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eleventh\">&nbsp;&#8220;What the heck is a &#8216;modulo&#8217;?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Remember when you first learned long division? You had the dividend, the divisor, and the remainder. The dividend is the number you start with. The divisor is the number you divide into the dividend. The remainder is what&#8217;s left. Modulo is a mathematical operator that returns the remainder.<\/p>\n<p>Here&#8217;s how it works. In the follow example, <code>@var1<\/code> serves as our dividend, and <code>@var2<\/code> serves as our divisor:<\/p>\n<pre wp-pre-tag-40=\"\"><\/pre>\n<p>The first expression in our <code>SELECT<\/code> clause uses the division operator (<code>\/<\/code>) to divide <code>@var1<\/code> by <code>@var2<\/code>. The second expression uses the modulo operator (<code>%<\/code>) to calculate the remainder that&#8217;s produced when you divide these two numbers. The following table shows the results.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>No surprise here. When you divide 32 by 8, you get 4, with a remainder of 0. But now let&#8217;s produce a remainder other than 0:<\/p>\n<pre wp-pre-tag-41=\"\"><\/pre>\n<p>This time around, we simply set <code>@var1<\/code> to equal 36, rather than 32. Now our results show a remainder of 4.<\/p>\n<p>That&#8217;s all there is to using the modulo operator. And you can use it with other numeric types:<\/p>\n<pre wp-pre-tag-42=\"\"><\/pre>\n<p>In this case, we&#8217;re dividing 55.566 by 5 to come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>11.111200000000000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0.5560<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, the expression that contains the modulo operator returns a value of <code>0.5560<\/code>. As for the expression that contains the division operator, the operator&#8217;s returned value is based on the argument type with the highest precedence, which in this case is <code>DECIMAL<\/code>.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"twelveth\">&#8220;Are all WHEN and ELSE clauses in a CASE expression evaluated if the first WHEN clause evaluates to TRUE?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>According to the SQL Server documentation, a <code>CASE<\/code> expression evaluates each condition sequentially and stops with the first condition that evaluates to true. In other words, the expression will short circuit when a condition is met, saving the database engine from having to perform unnecessary processing. This is a good thing. The less work the engine has to perform, the better.<\/p>\n<p>To get a sense of how this works, let&#8217;s look at an example. The following <code>SELECT<\/code> statement includes a <code>CASE<\/code> expression that sets up three possible conditions when concatenating the name-related columns in the <code>vSalesPerson<\/code> view:<\/p>\n<pre wp-pre-tag-43=\"\"><\/pre>\n<p>The first <code>CASE<\/code> condition checks whether the <code>MiddleName<\/code> value is <code>NULL<\/code>. If it is, the <code>FirstName<\/code> and <code>LastName<\/code> columns are concatenated and the expression stops running. If the column contains a value other than <code>NULL<\/code>, the expression moves on to the next condition, which checks the length of the <code>MiddleName<\/code> value. If it equals <code>1<\/code>, the three columns are concatenated and a period is added to the middle initial. The expression will then stop at that point. Otherwise, the <code>ELSE<\/code> condition is applied and all three columns are concatenated as is. The database engine repeats the process for each row and returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>CASE<\/code> expression&#8217;s ability to short circuit can help to avoid unnecessary processing when it can be substituted for other statement elements. For example, suppose we define the following <code>SELECT<\/code> statement:<\/p>\n<pre wp-pre-tag-44=\"\"><\/pre>\n<p>Nothing flashy here. For each row, the database engine tests for the three specified conditions and determines whether that row is returned. Consequently, the resultset includes any saleable product that is either black or red, which means 127 rows evaluate to true. Part of those results are shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 52<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 56<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Suppose we want to reduce some of the work that the database engine has to perform. We can instead use a <code>CASE<\/code> expression in the <code>WHERE<\/code> clause, as shown in the following <code>SELECT<\/code> statement:<\/p>\n<pre wp-pre-tag-45=\"\"><\/pre>\n<p>If the <code>Color<\/code> value equals <code>black<\/code>, the condition evaluates to true and the expression stops running, thus avoiding any unnecessary processing. Yet the statement returns the same results as the preceding example.<\/p>\n<p>Under the right circumstances, using a <code>CASE<\/code> expression in this way can be a handy way to reduce your workloads. However, a <code>CASE<\/code> expression might not always short circuit in the way you hope. For example, suppose your T-SQL includes logic is similar to that shown in the following example:<\/p>\n<pre wp-pre-tag-46=\"\"><\/pre>\n<p>All we&#8217;re doing is declaring an <code>INT<\/code> variable, setting its value to <code>0<\/code>, and using a <code>CASE<\/code> expression to retrieve a specific value. Based on our understanding of the <code>CASE<\/code> expression&#8217;s ability to short circuit, we would expect the expression&#8217;s first condition to evaluate to true and the expression to then stop. However, what we get from our <code>SELECT<\/code> statement is the following error:<\/p>\n<pre wp-pre-tag-47=\"\"><\/pre>\n<p>The error shows that the <code>ELSE<\/code> condition did in fact run, even though it shouldn&#8217;t have. It turns out that under certain circumstances, a <code>CASE<\/code> expression doesn&#8217;t behave like it&#8217;s supposed to behave. The problem appears to be related to aggregate functions (at least in part), so be aware that in some cases a <code>CASE<\/code> expression might not deliver the benefits you&#8217;re hoping for and you might have to come up with a workaround to make the <code>CASE<\/code> expression work the way you want it to.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"thirteenth\">&#8220;What&#8217;s the difference between the two &#8216;not equal to&#8217; operators (&lt;&gt; and !=)?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>In T-SQL, the two comparisons operators are syntactically equivalent. They each perform a Boolean comparison between two expressions. If those expressions return non-<code>NULL<\/code>, unequal values, the comparison evaluates to true, otherwise, it evaluates to false. However, if either value is <code> NULL<\/code>, the evaluation returns a <code>NULL<\/code>. Here&#8217;s a sample of the two operators in action:<\/p>\n<pre wp-pre-tag-48=\"\"><\/pre>\n<p>&nbsp;The variable is set to a value of <code> 1<\/code>, and in each <code>SELECT<\/code> statement, that variable is compared to the constant <code>2<\/code>. As a result, each <code>SELECT<\/code> statement returns the value <code>not<\/code> <code>equal<\/code> because each comparison evaluates to true. Even in terms of performance, there doesn&#8217;t appear to be much difference between the two.<\/p>\n<p>However, there is one difference you should be aware of. The <code>&lt;&gt;<\/code> operator conforms to ANSI standards; the <code>!=<\/code> operator does not, despite that fact that a number of relational database systems support both operators. That said, even if there is only the slightest chance that you might port your script to another system, you&#8217;re better off sticking with the <code> &lt;&gt;<\/code> operator.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fourteenth\">&#8220;I&#8217;ve come across operators such as += in variable assignment SET statements. What do they mean?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>The add-equals operator (<code>+=<\/code>) is a compound operator, one of several types of compound operators supported in SQL Server. A compound operator takes the original value and in some way amends it, rather than replacing it. For example, the following T-SQL uses the add-equals operator to update the value of the <code>@var1<\/code> variable:<\/p>\n<pre wp-pre-tag-49=\"\"><\/pre>\n<p>We start by declaring <code>@var1<\/code> and setting its value to <code>2<\/code>. We then use a <code>SET<\/code> statement to add a value of <code>1<\/code> to the original value. The <code>SELECT<\/code> statement returns the new value, which is <code>3<\/code>.<\/p>\n<p>The add-equals operator makes it possible to add a value without having to specify the original value. Without the add-equals operator we would have to rewrite our <code>SET<\/code> statement as follows:<\/p>\n<pre wp-pre-tag-50=\"\"><\/pre>\n<p>The add-equals operator merely provides the shorthand necessary to avoid having to explicitly specify the original value, yet the <code>SELECT<\/code> statement still returns a value of <code>3<\/code>. Plus, SQL Server supports other compound operators as well. For example, the following T-SQL subtracts <code>1<\/code> from the original value:<\/p>\n<pre wp-pre-tag-51=\"\"><\/pre>\n<p>In this case, we&#8217;re using the subtract-equals (<code>-=<\/code>) operator in our <code>SELECT<\/code> statement, rather than the add-equals operator. The subtract-equals operator works the same way as the add-equals operator, except that the second value is subtracted rather than added. As a result, our <code>SELECT<\/code> statement now returns a value of <code>1<\/code>.<\/p>\n<p>Another example of a compound operator is the multiply-equals operator, which is shown in the following example:<\/p>\n<pre wp-pre-tag-52=\"\"><\/pre>\n<p>In this case, the <code>SET<\/code> statement uses the multiply-equals operator to multiple the variable value <code>2<\/code> by the constant value <code>3<\/code>, giving us a total of <code>6<\/code>. And compound operators are not limited to mathematical expressions. For example, the following T-SQL includes a <code>SET<\/code> statement that uses the add-equals operator to concatenate values:<\/p>\n<pre wp-pre-tag-53=\"\"><\/pre>\n<p>In this example, the <code>SET<\/code> statement adds the string value <code>and<\/code> <code>then<\/code> <code>some<\/code> (including the opening space) to the variable value <code>some<\/code> <code>value<\/code>, giving us the value <code>some<\/code> <code>value<\/code> <code>and<\/code> <code>then<\/code> <code>some<\/code>. There are other compound operators as well, such as divide-equals (<code>\/=<\/code>) and modulo-equals (<code>%=<\/code>). Check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc645922.aspx\">Compound Operators<\/a>&#8221; for more information.<\/p>\n<\/div>\n\n\n\n<p>+ CONVERT(NVARCHAR(20), @var3, 1) AS amount3; &nbsp;<\/p>\n\n\n\n<p>Each expression in the <code>SELECT<\/code> list references one of the variables as its source data and is made up of multiple components. What provides the final format, however, is the <code>CONVERT<\/code> function. The function requires that the source data be either the <code>MONEY<\/code> or <code>SMALLMONEY<\/code> type. When you call the function, you specify a second argument along with the source value. That argument determines the format of the data. In this case, we specify <code>1<\/code> so the outputted data includes commas every three digits to the left of the decimal point. We also concatenate the value with a dollar sign to give it that final touch, as shown in the following results:<\/p>\n\n\n\n<figure class=\"wp-block-table MsoTableGrid\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p><b>amount1<\/b><\/p>\n<\/td><td>\n<p><b>amount2<\/b><\/p>\n<\/td><td>\n<p><b>amount3<\/b><\/p>\n<\/td><\/tr><tr><td>\n<p>$555,555,555.56<\/p>\n<\/td><td>\n<p>$555,555,555.56<\/p>\n<\/td><td>\n<p>$555,555,555.56<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&nbsp;Overall, this isn&#8217;t too painful a process, unless you&#8217;re working with currencies that follow a different format, such as the Swiss franc, which takes apostrophes rather than commas. In such cases, you might have to add yet another function, such as <code>REPLACE<\/code>, to substitute the commas with apostrophes.<\/p>\n\n\n\n<p>Fortunately, since the release of SQL Server 2012, you&#8217;ve been able to instead use the <code>FORMAT<\/code> function to output your numbers to a currency format. The <code>FORMAT<\/code> function returns a value in a specified format, based on a specified culture. To demonstrate how this works, let&#8217;s first recast our preceding expressions into ones that use the <code>FORMAT<\/code> function to configure the data as US dollar amounts:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>Notice that we must first cast the character data (<code>@var1<\/code>) to the <code>MONEY<\/code> data type, but we can use the <code>DECIMAL<\/code> variable (<code>@var2<\/code>) as is. The second <code>FORMAT<\/code> argument (<code>c<\/code>) specifies that we&#8217;re formatting currency, and the third argument specifies the US symbol (<code>en-us<\/code>) for the cultural context. The <code>SELECT<\/code> statement returns the same results as the previous <code>SELECT<\/code> statement, with all values returned as the <code>NVARCHAR<\/code> type.<\/p>\n\n\n\n<p>However, with the <code>FORMAT<\/code> function, we can specify other cultures. For example, the following example uses the German symbol (<code>de-de<\/code>) to provide the cultural context:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>Now our results are specific to how the euro is represented in Germany, as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table MsoTableGrid\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p><b>amount1<\/b><\/p>\n<\/td><td>\n<p><b>amount2<\/b><\/p>\n<\/td><td>\n<p><b>amount3<\/b><\/p>\n<\/td><\/tr><tr><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><td>\n<p>555.555.555,56 \u00e2\u00ac<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&nbsp;Certainly, the <code>FORMAT<\/code> function is a handy tool if you want your results to be displayed in a specific currency. But just to reiterate, such formatting is generally handled by the calling application, with the source data retrieved in its raw state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"third\">&#8220;I see the plus sign (+) used in a variety of ways in T-SQL script, but I can&#8217;t always make sense of how SQL Server arrives at the results it does. Is there a &#8220;right way&#8221; to use plus signs?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>SQL Server is a tricky devil. As you&#8217;ve discovered, plus signs have all sorts of meaning in T-SQL. You can use them to concatenate string values, add numerical values together, or indicate that a numerical value is a positive number, rather than negative. The following <code>SELECT<\/code> statement demonstrates how to both concatenate and add values:<\/p>\n<pre wp-pre-tag-4=\"\"><\/pre>\n<p>For the <code>FullName<\/code> column, we&#8217;ve used the plus sign to concatenate the <code>FirstName<\/code> and <code> LastName<\/code> columns with a space in between. For the <code>TwoYearSales<\/code> column, we&#8217;ve used the plus sign to add the <code>SalesLastYear<\/code> and <code> SalesYTD<\/code> columns together. In both cases, the database engine knows when to concatenate the values or when to add them together, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesLastYear<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesYTD<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>TwoYearSales<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,750,406.48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,763,178.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,513,584.66<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,439,156.03<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,251,368.55<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,690,524.58<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,997,186.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,189,418.37<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,186,604.57<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Garrett Vargas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,620,276.90<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,453,719.47<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,073,996.36<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,849,640.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,315,185.61<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,164,826.55<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,927,059.18<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,352,577.13<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,279,636.31<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,073,506.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,458,535.62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,532,041.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jos\u00e9 Saraiva<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,038,234.65<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,604,540.72<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,642,775.37<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,371,635.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,573,012.94<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,944,648.25<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$0.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,576,562.20<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Lynn Tsoflias<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,278,548.98<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,421,810.92<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,700,359.90<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Rachel Valdez<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,307,949.79<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,827,066.71<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,135,016.50<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jae Pak<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$1,635,823.40<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$4,116,871.23<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,752,694.62<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Ranjit Varkey Chudukatil<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$2,396,539.76<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$3,121,616.32<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$5,518,156.08<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There&#8217;s no real mystery here. When we use the plus sign with only character data, the database engine concatenates the values. In this case, the plus sign is considered a string concatenation operator. When we use the plus sign with numerical values, the database engine adds the values together. Under these circumstances, the plus sign is considered an addition operator.<\/p>\n<p>Not surprisingly, when we mix and match the types of data, our results are less predictable. Take a look at the following example:<\/p>\n<pre wp-pre-tag-5=\"\"><\/pre>\n<p>As you can see, we&#8217;ve declared seven variables, each defined with a different type. We then use the plus sign to combine the variables in different ways, giving us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>TestVarCX<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1299.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1001.0001<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2015-05-21 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2017-04-19 23:45:36.000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As we would expect, when we pair <code>@var1<\/code> with <code>@var2<\/code>, both character types, we end up with a concatenated string. When we pair <code>@var3<\/code> to <code>@var6<\/code>, both numerical types, the values are added together, even though <code>@var3<\/code> and <code>@var6<\/code> are different types.<\/p>\n<p>Now we get to our third pairing: <code>@var4<\/code> <code>+<\/code> <code>@var5<\/code>. This time around, we&#8217;re adding a <code>DECIMAL<\/code> value to <code>BIT<\/code> value. However, the database engine still treats both variables as numerical values and adds them together, even though SQL Server documentation suggests that you can&#8217;t do this with the <code>BIT<\/code> type. (And really, why would you?)<\/p>\n<p>The next item in the select list, <code>@var7<\/code> <code>+<\/code> <code>@var3<\/code>, pairs a <code>DATETIME<\/code> value with a <code>SMALLINT<\/code> value. This time, we get a date that is 300 days later than the original date. When your expression includes the plus sign, the database engine defaults to the data type with the highest precedence, in this case, <code> DATETIME<\/code>. Based on the type, the engine determines whether to use the plus sign to concatenate values or add them together. If the data type with the highest precedence is a numeric data type, the engine attempts to add the values. In this case, the engine adds the <code>SMALLINT<\/code> value to the <code>DATETIME<\/code> value as the specified number of days. (A <code>DATETIME<\/code> value is actually stored as two integers.)<\/p>\n<p>Finally, we pair a <code>MONEY<\/code> date type with the <code>DATETIME<\/code> date type (<code>@var6<\/code> <code>+<\/code> <code>@var7<\/code>). Once again, <code>DATETIME<\/code> takes precedence over <code>MONEY<\/code>, so the database engine adds 999.99 days to our original date to come up with a date in 2017.<\/p>\n<p>We can verify how data type precedence works with plus signs by using the <code>SQL_VARIANT_PROPERTY<\/code> function to retrieve the data type of the expressions that pair the different variables:<\/p>\n<pre wp-pre-tag-6=\"\"><\/pre>\n<p>The <code>SELECT<\/code> statement returns the returned data type for each expression, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3var6<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4var5<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var7var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var6var7<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>money<\/p>\n<\/td>\n<td valign=\"top\">\n<p>decimal<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;In each case, the expression returns the type with the highest precedence and determines how the plus sign will be used.<\/p>\n<p>So far in our examples, the database engine has been able to add or concatenate values without a problem because the engine could implicitly convert the data type with the lowest precedence to the type with the highest. But this isn&#8217;t always the case:<\/p>\n<pre wp-pre-tag-7=\"\"><\/pre>\n<p>This time around, we&#8217;re using the plus sign to pair a <code>NVARCHAR<\/code> value with a <code>SMALLINT<\/code> value. Because <code>SMALLINT<\/code> has precedence over <code>NVARCHAR<\/code>, the database engine attempts to convert the value <code>TestVar<\/code> to the <code>SMALLINT<\/code> type, which of course is not possible. Consequently, the database engine returns the following error message:<\/p>\n<pre wp-pre-tag-8=\"\"><\/pre>\n<p>The only way we can pair a numerical value with a string value is to concatenate them. In order to do so, we must explicitly convert the <code>SMALLINT<\/code> value to a character data type:<\/p>\n<pre wp-pre-tag-9=\"\"><\/pre>\n<p>Now our <code>SELECT<\/code> statement returns the value <code>TestVar300<\/code>.<\/p>\n<p>There&#8217;s one other use of the plus sign that you should be aware of-as a unary plus operator. SQL Server supports two types of unary operators, plus and minus, which are used to designate whether a numeric value is positive or negative:<\/p>\n<pre wp-pre-tag-10=\"\"><\/pre>\n<p>All we&#8217;ve done here is to designate our two literal values as positive and negative numbers. The <code>SELECT<\/code> statement returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>123.45<\/p>\n<\/td>\n<td valign=\"top\">\n<p>-123.45<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Be aware, however, that you cannot use the positive unary operator to convert a negative number to a positive number. For that, you need to use the <code>ABS<\/code> mathematical function.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fourth\">&#8220;The NULLIF expression makes no sense. I would think you&#8217;d use it to somehow ferret out a NULL value or to test a condition for a NULL value. But it appears to merely return a NULL if two values are equal. Is that all it&#8217;s doing?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>That&#8217;s part of what it&#8217;s doing. When a <code>NULLIF<\/code> expression compares two values, it returns the first value if the two values are not equal. If they are equal, the expression returns a <code>NULL<\/code> value of the same data type as the first specified value. Here&#8217;s what a <code>NULLIF<\/code> expression looks like in action:<\/p>\n<pre wp-pre-tag-11=\"\"><\/pre>\n<p>In this case, the <code>NULLIF<\/code> expression returns <code>valueA<\/code> because the two values are not equal. If they were equal, the returned value would be <code>NULL<\/code>. In addition, the returned value would be <code>NULL<\/code> if the first value were <code>NULL<\/code>.<\/p>\n<p>When you know how a <code>NULLIF<\/code> expression works, it doesn&#8217;t seem so bad, although its use still has a tendency to cause confusion. Luckily, we can achieve the same results by using a <code>CASE<\/code> statement:<\/p>\n<pre wp-pre-tag-12=\"\"><\/pre>\n<p>The advantage of the <code>CASE<\/code> statement is that it&#8217;s a lot easier to understand. Sure, <code>NULLIF<\/code> provides simpler syntax, but if it creates confusion, what&#8217;s the point?<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fifth\">&#8220;Can you use expressions in data manipulation language (DML) statements?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>You can use expressions wherever T-SQL syntax permits them, including DML statements. Take a look at part of the syntax for the <code>UPDATE<\/code> statement&#8217;s <code>SET<\/code> clause:<\/p>\n<pre wp-pre-tag-13=\"\"><\/pre>\n<p>&nbsp;According to SQL Server Books Online, the expression placeholder can be a &#8220;variable, literal value, expression, or a subselect statement (enclosed with parentheses) that returns a single value.&#8221; This pretty much defines the term <i>expression<\/i> as we understand it. Note, however, that any expression you define must conform to the rules associated with the particular T-SQL statement in which the expression is defined. For example, an <code>UPDATE<\/code> statement&#8217;s <code>UPDATE<\/code> clause can include the <code>TOP<\/code> clause, as shown in the following syntax:<\/p>\n<pre wp-pre-tag-14=\"\"><\/pre>\n<p>&nbsp;In this case, expression &#8220;specifies the number or percent of rows that will be updated.&#8221; In other words, you cannot specify a non-numerical string value as your expression, as you can with other types of expressions. That said, it&#8217;s still considered an expression.<\/p>\n<p>So, yes, you can use an expression in a DML statement. In fact, they&#8217;re used all the time. Let&#8217;s look at an example. Suppose we create the following table in our database and insert a row of data:<\/p>\n<pre wp-pre-tag-15=\"\"><\/pre>\n<p>Now let&#8217;s use an <code>UPDATE<\/code> statement to modify that row of data:<\/p>\n<pre wp-pre-tag-16=\"\"><\/pre>\n<p>Our <code>SET<\/code> clause is updating two columns. Each <code>SET<\/code> definition includes an expression that either adds the <code>@AmtReceived<\/code> value to the target column or subtracts that value. If we run the <code>SELECT<\/code> statement after the table has been updated, we come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>InStock<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>OnOrder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>67<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you&#8217;re writing T-SQL statements, you&#8217;re writing expressions, even if you don&#8217;t realize it. That&#8217;s why it&#8217;s important to refer back to the statement&#8217;s syntax whenever you&#8217;re uncertain how to define a particular element. Expressions are supported throughout most statements. The syntax for the <code>UPDATE<\/code> statement, for example, includes the expression placeholder in 10 different places, which suggests that many of us might not be using some statements to their fullest capacity.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"sixth\">&nbsp;&#8220;The CASE expression allows me to leave out the ELSE clause. Is it that important to include it?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Yes, but&#8230;. The <code>ELSE<\/code> clause specifies the value to return if no other comparison operations (<code>WHEN...THEN<\/code>) evaluate to true. If you omit the <code>ELSE<\/code> clause, the database engine instead returns a <code>NULL<\/code> value. A <code>NULL<\/code> value might be fine, if that&#8217;s what you want. But what if it&#8217;s not? Take a look at the following example, which uses a <code>CASE<\/code> expression to determine how to return an employee&#8217;s full name:<\/p>\n<pre wp-pre-tag-17=\"\"><\/pre>\n<p>If the <code>MiddleName<\/code> column is <code>NULL<\/code>, that column is not included in the expression. Only the first and last names are concatenated. If the <code>MiddleName<\/code> value is an initial, a period is added to the initial and concatenated with the first and last names. Otherwise, all three names are concatenated, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now let&#8217;s modify the <code>CASE<\/code> expression by removing the <code>ELSE<\/code> clause:<\/p>\n<pre wp-pre-tag-18=\"\"><\/pre>\n<p>This time a <code>NULL<\/code> is returned if the <code>MiddleName<\/code> column has a value that is more than an initial, as the following table shows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly, you would not want to delete the <code>ELSE<\/code> clause in this instance. You could, however, add a <code>WHEN...THEN<\/code> condition that replaces the <code>ELSE<\/code> clause, in which case, you&#8217;re still returning <code>NULL<\/code> of none of the conditions are met, which might be fine under some circumstances. Consider the following example:<\/p>\n<pre wp-pre-tag-19=\"\"><\/pre>\n<p>If the <code>SalesQuota<\/code> value is great than 250,000, a <code>true<\/code> is returned. If the value is less than or equal to 250,000, we get a <code>false<\/code>. However, if the column is <code>NULL<\/code>, neither condition can be true, so the <code>CASE<\/code> expression returns a <code>NULL<\/code>, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>HigherQuota<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Stephen Jiang<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Michael Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Linda Mitchell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tsvi Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Pamela Ansman-Wolfe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shu Ito<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>David Campbell<\/p>\n<\/td>\n<td valign=\"top\">\n<p>false<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Tete Mensa-Annan<\/p>\n<\/td>\n<td valign=\"top\">\n<p>true<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Syed Abbas<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Amy Alberts<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can choose to include an <code>ELSE<\/code> clause and then provide a default value, such as <code>N\/A<\/code> (or even <code>NULL<\/code>), whether or not you think it&#8217;s necessary. In fact, many developers think you should never omit the <code>ELSE<\/code> clause, even if you believe it is impossible for one of the <code>WHEN...THEN<\/code> conditions not to be met. Someone might make a change to the database that affects the statement or your thinking about possible outcomes might not be correct.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"seventh\">&#8220;Can you shed light on the order in which the database engine processes the components in an expression?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>The way in which an expression&#8217;s elements are processed depends on several factors: their listed order, whether any subset of elements is enclosed in parentheses, and what operators are used to connect the various elements. Parentheses group elements together to ensure that those elements are processed as a unit before being incorporated into the rest of the expression. In addition, the expression as a whole adheres to the concept of <i>operator precedence,<\/i> in which the operator types determine the order in which the expression is evaluated. For example, the multiply and division arithmetic operators take precedence over the greater than (<code>&gt;<\/code>) and lesser than (<code>&lt;<\/code>) comparison operations, which take precedence over logical operators such as <code>AND<\/code> or <code>ANY<\/code>. (You can find details about operator precedence in the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190276.aspx\">Operator Precedence<\/a>.&#8221;)<\/p>\n<p>Let&#8217;s start with a few simple arithmetic-based expressions to give you a sense of how this works. The following example declares several <code>INT<\/code> variables, assigns an expression to each one, and returns the values from all three:<\/p>\n<pre wp-pre-tag-20=\"\"><\/pre>\n<p>All we&#8217;re doing in each expression is multiplying and adding and subtracting values. However, because we use parentheses in the second and third expressions, our results come up quite different:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>num1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>num3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>@num1<\/code> expression is fairly straightforward. We multiple 3 by 2, add 5, and subtract 7, which gives us a total of 4.<\/p>\n<p>However, in the <code>@num2<\/code> expression, we enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> elements in parentheses, so they&#8217;re processed first, giving us a total of 7. The 7 is then multiplied by the first 3, giving us 21. From there, we subtract 7, giving us a total of 14.<\/p>\n<p>In the <code>@num3<\/code> expression, the parentheses enclose the <code>2<\/code> <code>+<\/code> <code>5<\/code> <code>-<\/code> <code>7<\/code> elements, which are processed before the other elements. Because this returns 0, 3 is multiplied by 0, giving us a final result of 0.<\/p>\n<p>Now let&#8217;s look at another example, which again declares three variables and applies different expressions to them:<\/p>\n<pre wp-pre-tag-21=\"\"><\/pre>\n<p>Here&#8217;s what are results look like:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>SalesDiff1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SalesDiff2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6500<\/p>\n<\/td>\n<td valign=\"top\">\n<p>500<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;The first expression includes no parentheses. However, the multiply operator takes precedence over the addition and subtraction operators, so the multiplication is performed first. That means we first multiply the <code>SalesQuota<\/code> value by 2 and the <code>SalesYTD<\/code> value by 2, giving us the expression <code>6000<\/code> <code>-<\/code> <code>2500<\/code> <code>+<\/code> <code>3000<\/code>, which equals 6500.<\/p>\n<p>The second expression does use parentheses. The first set of parentheses returns a value of 6000. The second set of parentheses actually contains a third set, and those are the elements evaluated first, before the outer set of elements. After they&#8217;re evaluated, that part of the expression enclosed in the second set of parentheses becomes <code>@SalesLastYear<\/code> <code> +<\/code> <code>3000<\/code>, giving us a total of 5500. That 5500 is then subtracted from the 6000, which is how we end up with 500.<\/p>\n<p>Now let&#8217;s look at an expression that incorporates character data as well as numerical data. In the following <code>SELECT<\/code> statement, our <code>WHERE<\/code> clause includes both the <code>OR<\/code> and the <code>AND<\/code> logical operators:<\/p>\n<pre wp-pre-tag-22=\"\"><\/pre>\n<p>The <code>WHERE<\/code> clause search condition contains three predicates separated by the <code>AND<\/code> operator and the <code>OR<\/code> operator. In its current state, the search condition states that the <code>ListPrice<\/code> value must be greater than 2.5 times the <code>StandardCost<\/code> value <i>and<\/i> the <code>ProductLine<\/code> value must equal <code>r<\/code> <i>or<\/i> the <code>ProductLine<\/code> value must equal <code>t<\/code>. According to the rules of operator precedence, the database engine first evaluates the <code>AND<\/code> logical operator and then the <code>OR<\/code> logical operator. Because of the placement of the operators, the first two predicates must both evaluate to true <i>or<\/i> the third predicate must evaluate to true. In other words, all returned rows must meet the first two conditions or meet the third condition. As a result, the statement returns 59 rows that qualify. The following table shows part of the results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Front Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>96.7964<\/p>\n<\/td>\n<td valign=\"top\">\n<p>218.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>108.7844<\/p>\n<\/td>\n<td valign=\"top\">\n<p>245.01<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-Panniers, Large<\/p>\n<\/td>\n<td valign=\"top\">\n<p>51.5625<\/p>\n<\/td>\n<td valign=\"top\">\n<p>125.00<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Touring Frame &#8211; Yellow, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>199.8519<\/p>\n<\/td>\n<td valign=\"top\">\n<p>333.42<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Yellow, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>601.7437<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Suppose what we were really after was to return rows that met the first condition <i>and<\/i> either the second <i>or<\/i> third condition. In this case, we would have to modify our search condition slightly:<\/p>\n<pre wp-pre-tag-23=\"\"><\/pre>\n<p>Notice that the second and third predicates are now enclosed in parentheses. Consequently, the database engine will first evaluate them as a unit. That means for all rows returned, <code>ListPrice<\/code> must be greater than 2.5 times the value of <code>StandardCost<\/code> <i>and<\/i> <code>ProductLine<\/code> must equal <code>r<\/code> <i>or<\/i> <code>t<\/code>. The following table shows all the results now returned by the query.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>StandardCost<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ProductLine<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Bottle Cage<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Racing Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.3623<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Road Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.4923<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire Tube<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1.8663<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8.0373<\/p>\n<\/td>\n<td valign=\"top\">\n<p>21.49<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ML Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>9.3463<\/p>\n<\/td>\n<td valign=\"top\">\n<p>24.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12.1924<\/p>\n<\/td>\n<td valign=\"top\">\n<p>32.60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>R<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring Tire<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.8423<\/p>\n<\/td>\n<td valign=\"top\">\n<p>28.99<\/p>\n<\/td>\n<td valign=\"top\">\n<p>T<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Notice that we now have only nine rows and that each row meets both conditions for the <code>ListPrice<\/code> and <code>ProductLine<\/code> columns. By applying the parentheses, we have better controlled how the rules of operator precedence are applied.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eighth\">&#8220;I get confused on how to treat constants in my expressions. Is there a trick in using them?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>A constant is a literal value that is either part of an expression or is the entire expression. The way in which you treat constants depends on the type of data. As a general rule, you enclose character data in single quotes, but leave numerical data without quotes, although it&#8217;s not quite as straightforward as this. Before we go into the oddities, though, let&#8217;s look at a few examples of character constants. The following T-SQL declares several character variables and then retrieves their values:<\/p>\n<pre wp-pre-tag-24=\"\"><\/pre>\n<p>By default, when you&#8217;re working with character string constants, you enclose them in singe quotes. If you&#8217;re working with Unicode strings, you precede the opening quote with an uppercase N. Anything enclosed in the quotes is considered part of that string, even if the characters are numbers, operators, or other types of symbols, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>some value<\/p>\n<\/td>\n<td valign=\"top\">&nbsp;<\/td>\n<td valign=\"top\">\n<p>some value + 1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>12345<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;In SQL Server, we can instead enclose character strings in double quotes by setting the <code>QUOTED_IDENTIFIER<\/code> option to <code> OFF<\/code>, as shown in the following example:<\/p>\n<pre wp-pre-tag-25=\"\"><\/pre>\n<p>When the <code>QUOTED_IDENTIFIER<\/code> option is set to <code>OFF<\/code>, we can use double quotes just like single quotes, as is the case with the <code>@var1<\/code> declaration. The advantage of using double quotes is that you can pass in a special character, such as an apostrophe, without having to escape it by adding a second apostrophe, as we had to do for <code>@var3<\/code>. The <code>SELECT<\/code> statement now returns the results shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>its value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>it&#8217;s a value<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Be aware, however, that the <code>QUOTED_IDENTIFIER<\/code> option has other implications besides being able to use double quotes for string constants. For example, the option affects the way identifiers are handled in T-SQL. In addition, the SQL Server Native Client Provider and ODBC driver set this option to <code>ON<\/code>, which can impact your code if you had set the option to <code>OFF<\/code>. For these reasons you&#8217;re usually better off leaving the option set to <code>ON<\/code>.<\/p>\n<p>As previously mentioned, you typically don&#8217;t enclose numerical constants in quotes, even if they include currency symbols or scientific notation. For example, the following T-SQL declares four types of numerical variables, sets their values, and retrieves those values:<\/p>\n<pre wp-pre-tag-26=\"\"><\/pre>\n<p>If we had enclosed the constants in quotes, the database engine would have interpreted the values as character data and have tried to convert it to the type of the applicable variable. For the first three variables, an implicit conversion would have worked fine, but it would mean unnecessary work on the part of the database engine. For the last variable, the engine would have returned the following error:<\/p>\n<pre wp-pre-tag-27=\"\"><\/pre>\n<p>When we specify a numerical constant without quotes, the database engine assumes a numerical type consistent with the value. For example, the value <code>147<\/code> is assumed to be an <code>INT<\/code>, so no implicit conversion is necessary. The following table shows the results returned by the <code> SELECT<\/code> statement.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>147<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10.5800<\/p>\n<\/td>\n<td valign=\"top\">\n<p>68.24<\/p>\n<\/td>\n<td valign=\"top\">\n<p>87.9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Even with numeric constants, an implicit conversion might be called for. In the following <code>SELECT<\/code> statement, the constant <code>1000<\/code> is compared the value in the <code>ListPrice<\/code> column, which is configured with the <code>MONEY<\/code> data type:<\/p>\n<pre wp-pre-tag-28=\"\"><\/pre>\n<p>The <code>1000<\/code> is treated as an <code>INT<\/code> value, so it must be implicitly converted to the <code>MONEY<\/code> type. But that&#8217;s not all what&#8217;s going on in the <code>WHERE<\/code> clause. Notice that it also includes a string constant (<code>blue<\/code>). The constant is compared to the <code>Color<\/code> column, which is configured with the <code>NVARCHAR<\/code> data type. This means that the database engine must convert the <code>VARCHAR<\/code> string to the <code>NVARCHAR<\/code> type, which gives us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>ListPrice<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Touring Frame &#8211; Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1003.91<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-1000 Blue, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2384.07<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 50<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Touring-2000 Blue, 54<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1214.85<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Like string constants, date and time values are also enclosed in single quotes, even though the values are stored as integers, as you can see in the following example:<\/p>\n<pre wp-pre-tag-29=\"\"><\/pre>\n<p>The date and time data types will accept various formats when passing in a constant, as long as the value is enclosed in quotes. The following table shows the results returned by the <code>SELECT<\/code> statement:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2014-08-01 00:00:00.000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-28 16:24:27.293<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2014-07-30<\/p>\n<\/td>\n<td valign=\"top\">\n<p>14:05:55.0010000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;You should also be aware of how to treat constants when working with some of the other data types. For example, a <code>VARBINARY<\/code> value takes an <code>0x<\/code> prefix but is not enclosed in quotes. The same goes for a <code>BIT<\/code> value. However, a <code>UNIQUEIDENTIFIER<\/code> value is enclosed in quotes, as shown in the following T-SQL:<\/p>\n<pre wp-pre-tag-30=\"\"><\/pre>\n<p>&nbsp;Our <code>SELECT<\/code> statement now returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>var1<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var2<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>var3<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>0x14AD<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CC421A37-E462-4AE0-8451-38F837FC5A1A<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Again, we generally enclose string values in quotes and leave the quotes off for numerical values, but as you&#8217;ve seen, some areas can be somewhat gray. If you get confused, check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179899.aspx\">Constants<\/a>.&#8221;<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ninth\">&#8220;Are there any advantages to using a COALESCE expression rather than a CASE expression when checking for the first non-NULL value in a list of values?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>A <code>COALESCE<\/code> expression is a syntactic shortcut for a <code>CASE<\/code> expression. That means, when the query optimizer gets ahold of a <code>COALESCE<\/code> expression, it rewrites it as a <code>CASE<\/code> one, so in that sense, they&#8217;re one in the same. Let&#8217;s look at a couple of examples. In the first one, we use <code>COALESCE<\/code> to return the first column that is not a <code>NULL<\/code> value:<\/p>\n<pre wp-pre-tag-31=\"\"><\/pre>\n<p>For this particular product, the <code>Size<\/code> column and <code>SizeUnitMeasureCode<\/code> column each contains a <code>NULL<\/code> value, but the <code>WeightUnit<\/code> column contains an actual value, <code>G<\/code>, so the <code>COALESCE<\/code> expression returns that value, as shown in the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Size<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>SizeUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>WeightUnit<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FirstNotNull<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Rear Wheel<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G&nbsp;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>G&nbsp;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Now let&#8217;s look at what happens when we change the <code> COALESCE<\/code> expression to a <code>CASE<\/code> expression:<\/p>\n<pre wp-pre-tag-32=\"\"><\/pre>\n<p>The <code>SELECT<\/code> statement returns the same results as the previous example. That said, the <code>COALESCE<\/code> expression is a lot simpler, although the logic of the <code>CASE<\/code> statement is easier to understand. In some cases, you might find that it&#8217;s better to use <code>CASE<\/code> so your intent is clear to other developers.<\/p>\n<p>One other consideration with <code>COALESCE<\/code>. Developers sometimes run into problems with <code>COALESCE<\/code> expressions because of data type issues. The value returned by <code>COALESCE<\/code> is based on the data type of the input value with the highest precedence, which might or might not be the returned value. If an implicit conversion is required, and such a conversion is not permitted, <code>COALESCE<\/code> will return an error. See the article &#8220;<a href=\"https:\/\/www.simple-talk.com\/sql\/t-sql-programming\/questions-about-sql-server-data-types-you-were-too-shy-to-ask\/\">Questions about SQL Server Data Types You were Too Shy to Ask<\/a>&#8221; for more details about this particular issue.<\/p>\n<p>All that said, figuring out the logic behind <code>COALESCE<\/code> is not too difficult, so choosing between <code>CASE<\/code> and <code>COALESCE<\/code> often comes down to nothing more than personal preference.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"tenth\">&#8220;I&#8217;m working on a query that uses the NOT IN operator in the WHERE clause. The operator checks values in a list returned by a subquery. When the returned list includes a NULL value, the query returns an empty resultset, even though I know I should be seeing results. Any idea what might be happening?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Using <code>NOT<\/code> <code>IN<\/code> can be tricky, especially when <code>NULL<\/code> values are involved. Case in point. The following <code>SELECT<\/code> statement includes a <code>WHERE<\/code> clause that checks for specific colors:<\/p>\n<pre wp-pre-tag-33=\"\"><\/pre>\n<p>Basically, what we&#8217;re doing is returning all products that are saleable items (<code>FinishedGoodsFlag<\/code> <code>=<\/code> <code>1<\/code>) and whose color is not one of the colors of the non-saleable products. Unfortunately, when we run this query we receive an empty resultset.<\/p>\n<p>If you&#8217;re familiar with the data, you might be surprised by these results. After all, the table contains 295 rows of saleable products. Either there are no colors unique to saleable items, or something is wrong.<\/p>\n<p>We can investigate the problem by first running the subquery in the example above separate from the outer query:<\/p>\n<pre wp-pre-tag-34=\"\"><\/pre>\n<p>The following table shows the subquery&#8217;s results, which includes three distinct items:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, we have black and silver non-saleable items, along with those items for which no color has been assigned (the <code>NULL<\/code> value). Now let&#8217;s modify the subquery to instead retrieve the colors of the orderable products:<\/p>\n<pre wp-pre-tag-35=\"\"><\/pre>\n<p>This time around, we again receive the same three values, along with another seven colors, as shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Grey<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Silver\/Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Yellow<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clearly something is wrong. Rows that contain those seven additional colors should have been included in our original query results. The problem, it turns out, is the <code>NULL<\/code> value. Let&#8217;s recast the logic of the <code>WHERE<\/code> clause into several separate predicates, connected by the <code>AND<\/code> logical operator:<\/p>\n<pre wp-pre-tag-36=\"\"><\/pre>\n<p>Essentially, our <code>NOT<\/code> <code>IN<\/code> operator indicates that the value in the <code>Color<\/code> column should not be <code>Black<\/code> <i>and<\/i> should not be <code>Silver<\/code> <i>and<\/i> should not be <code> NULL<\/code>, in addition to being a saleable product. The problem is with the equation <code>Color<\/code> <code>&lt;&gt;<\/code> <code>NULL<\/code>. For a row to be returned, all conditions must evaluate to true; however, a <code>NULL<\/code> cannot evaluate to true or false, which means the condition as a whole will never evaluate to true and no rows will ever be returned. If we were to run this statement, we would once again receive an empty resultset.<\/p>\n<p>One way around this predicament is to add a second predicate to the <code>WHERE<\/code> clause in our subquery:<\/p>\n<pre wp-pre-tag-37=\"\"><\/pre>\n<p>Now our subquery returns only the values <code>Black<\/code> and <code>Silver<\/code>, which means our outer query should return all rows except those whose <code>Color<\/code> value is one of the two colors. The following table shows part of the results, although the query actually returns 120 rows:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Mountain Bike Socks, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>White<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Blue<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Blue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AWC Logo Cap<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, S<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, M<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, L<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Long-Sleeve Logo Jersey, XL<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Multi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you were to run this query, you would see the <code>Color<\/code> values <code>black<\/code> and <code>silver<\/code> are not included. However, what&#8217;s odd about this is that rows with <code>NULL<\/code> for a color are not included either, even though <code>NULL<\/code> is no longer being returned by the subquery. This might be fine in some cases, but in others you might want to see those values. Before we start looking for a workaround, however, another consideration is performance. Many SQL Server folks recommend using an <code>EXISTS<\/code> operator rather than an <code>IN<\/code> operator anyway. So let&#8217;s rewrite the last statement using <code>NOT<\/code> <code>EXISTS<\/code>:<\/p>\n<pre wp-pre-tag-38=\"\"><\/pre>\n<p>Our <code>WHERE<\/code> clause now specifies that we include only saleable products, that they be a known color, and that the color not exist within the list of non-orderable colors. As a result, the <code>SELECT<\/code> statement returns the same results as the preceding one, 120 rows. And if we want to include the saleable products with a <code>Color<\/code> value of <code>NULL<\/code>, we can simply remove the <code>IS<\/code> <code>NOT<\/code> <code>NULL<\/code> expression:<\/p>\n<pre wp-pre-tag-39=\"\"><\/pre>\n<p>Our statement now returns 170 rows, which include those rows with a <code>Color<\/code> value of <code>NULL<\/code>. In theory, this statement should perform better than the ones using <code>NOT<\/code> <code>NULL<\/code>, but you might want to test that part out yourself to be sure. Just know that using <code>NOT<\/code> <code>IN<\/code> with <code>NULL<\/code> values can return unexpected results, so be prepared.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eleventh\">&nbsp;&#8220;What the heck is a &#8216;modulo&#8217;?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>Remember when you first learned long division? You had the dividend, the divisor, and the remainder. The dividend is the number you start with. The divisor is the number you divide into the dividend. The remainder is what&#8217;s left. Modulo is a mathematical operator that returns the remainder.<\/p>\n<p>Here&#8217;s how it works. In the follow example, <code>@var1<\/code> serves as our dividend, and <code>@var2<\/code> serves as our divisor:<\/p>\n<pre wp-pre-tag-40=\"\"><\/pre>\n<p>The first expression in our <code>SELECT<\/code> clause uses the division operator (<code>\/<\/code>) to divide <code>@var1<\/code> by <code>@var2<\/code>. The second expression uses the modulo operator (<code>%<\/code>) to calculate the remainder that&#8217;s produced when you divide these two numbers. The following table shows the results.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>No surprise here. When you divide 32 by 8, you get 4, with a remainder of 0. But now let&#8217;s produce a remainder other than 0:<\/p>\n<pre wp-pre-tag-41=\"\"><\/pre>\n<p>This time around, we simply set <code>@var1<\/code> to equal 36, rather than 32. Now our results show a remainder of 4.<\/p>\n<p>That&#8217;s all there is to using the modulo operator. And you can use it with other numeric types:<\/p>\n<pre wp-pre-tag-42=\"\"><\/pre>\n<p>In this case, we&#8217;re dividing 55.566 by 5 to come up with the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>Division<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Remainder<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>11.111200000000000<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0.5560<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, the expression that contains the modulo operator returns a value of <code>0.5560<\/code>. As for the expression that contains the division operator, the operator&#8217;s returned value is based on the argument type with the highest precedence, which in this case is <code>DECIMAL<\/code>.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"twelveth\">&#8220;Are all WHEN and ELSE clauses in a CASE expression evaluated if the first WHEN clause evaluates to TRUE?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>According to the SQL Server documentation, a <code>CASE<\/code> expression evaluates each condition sequentially and stops with the first condition that evaluates to true. In other words, the expression will short circuit when a condition is met, saving the database engine from having to perform unnecessary processing. This is a good thing. The less work the engine has to perform, the better.<\/p>\n<p>To get a sense of how this works, let&#8217;s look at an example. The following <code>SELECT<\/code> statement includes a <code>CASE<\/code> expression that sets up three possible conditions when concatenating the name-related columns in the <code>vSalesPerson<\/code> view:<\/p>\n<pre wp-pre-tag-43=\"\"><\/pre>\n<p>The first <code>CASE<\/code> condition checks whether the <code>MiddleName<\/code> value is <code>NULL<\/code>. If it is, the <code>FirstName<\/code> and <code>LastName<\/code> columns are concatenated and the expression stops running. If the column contains a value other than <code>NULL<\/code>, the expression moves on to the next condition, which checks the length of the <code>MiddleName<\/code> value. If it equals <code>1<\/code>, the three columns are concatenated and a period is added to the middle initial. The expression will then stop at that point. Otherwise, the <code>ELSE<\/code> condition is applied and all three columns are concatenated as is. The database engine repeats the process for each row and returns the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>EmployeeID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>FullName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Territory<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>275<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Michael G. Blythe<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Northeast<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>277<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Jillian Carson<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Central<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>279<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Tsvi Michael Reiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Southeast<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>CASE<\/code> expression&#8217;s ability to short circuit can help to avoid unnecessary processing when it can be substituted for other statement elements. For example, suppose we define the following <code>SELECT<\/code> statement:<\/p>\n<pre wp-pre-tag-44=\"\"><\/pre>\n<p>Nothing flashy here. For each row, the database engine tests for the three specified conditions and determines whether that row is returned. Consequently, the resultset includes any saleable product that is either black or red, which means 127 rows evaluate to true. Part of those results are shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>ProductName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Color<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Red<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Sport-100 Helmet, Black<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 44<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 48<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 52<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>HL Road Frame &#8211; Red, 56<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Red<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 58<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 60<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LL Road Frame &#8211; Black, 62<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Black<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;Suppose we want to reduce some of the work that the database engine has to perform. We can instead use a <code>CASE<\/code> expression in the <code>WHERE<\/code> clause, as shown in the following <code>SELECT<\/code> statement:<\/p>\n<pre wp-pre-tag-45=\"\"><\/pre>\n<p>If the <code>Color<\/code> value equals <code>black<\/code>, the condition evaluates to true and the expression stops running, thus avoiding any unnecessary processing. Yet the statement returns the same results as the preceding example.<\/p>\n<p>Under the right circumstances, using a <code>CASE<\/code> expression in this way can be a handy way to reduce your workloads. However, a <code>CASE<\/code> expression might not always short circuit in the way you hope. For example, suppose your T-SQL includes logic is similar to that shown in the following example:<\/p>\n<pre wp-pre-tag-46=\"\"><\/pre>\n<p>All we&#8217;re doing is declaring an <code>INT<\/code> variable, setting its value to <code>0<\/code>, and using a <code>CASE<\/code> expression to retrieve a specific value. Based on our understanding of the <code>CASE<\/code> expression&#8217;s ability to short circuit, we would expect the expression&#8217;s first condition to evaluate to true and the expression to then stop. However, what we get from our <code>SELECT<\/code> statement is the following error:<\/p>\n<pre wp-pre-tag-47=\"\"><\/pre>\n<p>The error shows that the <code>ELSE<\/code> condition did in fact run, even though it shouldn&#8217;t have. It turns out that under certain circumstances, a <code>CASE<\/code> expression doesn&#8217;t behave like it&#8217;s supposed to behave. The problem appears to be related to aggregate functions (at least in part), so be aware that in some cases a <code>CASE<\/code> expression might not deliver the benefits you&#8217;re hoping for and you might have to come up with a workaround to make the <code>CASE<\/code> expression work the way you want it to.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"thirteenth\">&#8220;What&#8217;s the difference between the two &#8216;not equal to&#8217; operators (&lt;&gt; and !=)?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>In T-SQL, the two comparisons operators are syntactically equivalent. They each perform a Boolean comparison between two expressions. If those expressions return non-<code>NULL<\/code>, unequal values, the comparison evaluates to true, otherwise, it evaluates to false. However, if either value is <code> NULL<\/code>, the evaluation returns a <code>NULL<\/code>. Here&#8217;s a sample of the two operators in action:<\/p>\n<pre wp-pre-tag-48=\"\"><\/pre>\n<p>&nbsp;The variable is set to a value of <code> 1<\/code>, and in each <code>SELECT<\/code> statement, that variable is compared to the constant <code>2<\/code>. As a result, each <code>SELECT<\/code> statement returns the value <code>not<\/code> <code>equal<\/code> because each comparison evaluates to true. Even in terms of performance, there doesn&#8217;t appear to be much difference between the two.<\/p>\n<p>However, there is one difference you should be aware of. The <code>&lt;&gt;<\/code> operator conforms to ANSI standards; the <code>!=<\/code> operator does not, despite that fact that a number of relational database systems support both operators. That said, even if there is only the slightest chance that you might port your script to another system, you&#8217;re better off sticking with the <code> &lt;&gt;<\/code> operator.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fourteenth\">&#8220;I&#8217;ve come across operators such as += in variable assignment SET statements. What do they mean?&#8221;<\/h3>\n\n\n\n<div class=\"indented\">\n<p>The add-equals operator (<code>+=<\/code>) is a compound operator, one of several types of compound operators supported in SQL Server. A compound operator takes the original value and in some way amends it, rather than replacing it. For example, the following T-SQL uses the add-equals operator to update the value of the <code>@var1<\/code> variable:<\/p>\n<pre wp-pre-tag-49=\"\"><\/pre>\n<p>We start by declaring <code>@var1<\/code> and setting its value to <code>2<\/code>. We then use a <code>SET<\/code> statement to add a value of <code>1<\/code> to the original value. The <code>SELECT<\/code> statement returns the new value, which is <code>3<\/code>.<\/p>\n<p>The add-equals operator makes it possible to add a value without having to specify the original value. Without the add-equals operator we would have to rewrite our <code>SET<\/code> statement as follows:<\/p>\n<pre wp-pre-tag-50=\"\"><\/pre>\n<p>The add-equals operator merely provides the shorthand necessary to avoid having to explicitly specify the original value, yet the <code>SELECT<\/code> statement still returns a value of <code>3<\/code>. Plus, SQL Server supports other compound operators as well. For example, the following T-SQL subtracts <code>1<\/code> from the original value:<\/p>\n<pre wp-pre-tag-51=\"\"><\/pre>\n<p>In this case, we&#8217;re using the subtract-equals (<code>-=<\/code>) operator in our <code>SELECT<\/code> statement, rather than the add-equals operator. The subtract-equals operator works the same way as the add-equals operator, except that the second value is subtracted rather than added. As a result, our <code>SELECT<\/code> statement now returns a value of <code>1<\/code>.<\/p>\n<p>Another example of a compound operator is the multiply-equals operator, which is shown in the following example:<\/p>\n<pre wp-pre-tag-52=\"\"><\/pre>\n<p>In this case, the <code>SET<\/code> statement uses the multiply-equals operator to multiple the variable value <code>2<\/code> by the constant value <code>3<\/code>, giving us a total of <code>6<\/code>. And compound operators are not limited to mathematical expressions. For example, the following T-SQL includes a <code>SET<\/code> statement that uses the add-equals operator to concatenate values:<\/p>\n<pre wp-pre-tag-53=\"\"><\/pre>\n<p>In this example, the <code>SET<\/code> statement adds the string value <code>and<\/code> <code>then<\/code> <code>some<\/code> (including the opening space) to the variable value <code>some<\/code> <code>value<\/code>, giving us the value <code>some<\/code> <code>value<\/code> <code>and<\/code> <code>then<\/code> <code>some<\/code>. There are other compound operators as well, such as divide-equals (<code>\/=<\/code>) and modulo-equals (<code>%=<\/code>). Check out the MSDN topic &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc645922.aspx\">Compound Operators<\/a>&#8221; for more information.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Nobody seems to ask questions about SQL Expressions in forums, even though expressions can cause all sorts of problems. Even the books on T-SQL skate over them in haste to get to more complicated topics. It is time for frank, straight-forward Q&amp;A, and who better than Robert Sheldon to give the A?&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143531],"tags":[4150,4183,4252,5771],"coauthors":[6779],"class_list":["post-1854","post","type-post","status-publish","format-standard","hentry","category-t-sql-programming-sql-server","tag-sql","tag-t-sql","tag-t-sql-programming","tag-too-shy"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1854"}],"version-history":[{"count":9,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1854\/revisions"}],"predecessor-version":[{"id":110603,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1854\/revisions\/110603"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1854"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}