Friday, October 23, 2009

Grecian 2000 Lady Lotion Side Effects

Functions to work with NULL values \u200b\u200bin SQL Server Tools

months ago deepen on the subject of postie NULL values \u200b\u200bbut not forgetfulness, carelessness or whatever ... but yesterday I got to optimize and make a few select's with null values \u200b\u200b.. hopefully something they can use the following info. To make the examples we first intalar AdventureWorks database SQL Server can

produce unexpected results when performing the calculations in columns that have NULL values \u200b\u200b . In such situations, typically assigns a value to 0 to replace the columns that contain NULL values \u200b\u200b . This will yield more unexpected results. Therefore, to solve this problem you need to use ISNULL functions, NULLIF and COALESCE SQL Server 2005, when its columns contain NULL values.

  • ISNULL function replaces NULL values \u200b\u200bwith the specified replacement value. The following is the ISNULL function syntax:

check_expression ISNULL (replacement_value)
The function takes the following arguments:

check_expression is the expression to be checked for NULL values \u200b\u200band can be any type of data.

replacement_value is the expression returned if check_expression is NULL and should be the same data type as the check_expression.

ISNULL function returns the result that the same type as the check_expression.

The following code example demonstrates the use of ISNULL function in a SELECT statement as well as the function is not null is the replacement value. Execute the judgments and we will see the difference in value

resulting


USE Adventureworks ;
SELECT AVG ( Weight) FROM
Production. Product

USE Adventureworks ;
SELECT AVG ( ISNULL (Weight , 50 )) FROM
Production . Product

In this example, the ISNULL function is used to find the average weight of all products. ISNULL function replaces the value of 50 for all NULL entries in the column of the table weight products.

The following is the full result set the query.

59.79 (1 rows affected)

  • NULLIF function returns NULL if the two specified expressions are equivalent. The following is the syntax of the NULLIF function: NULLIF

(expr, expression)

The function takes two arguments. The argument expression can be a constant, column name, subquery, function, or any combination of arithmetic, bitwise and string operators. Function NULLIF returns the result of the same data type as the first expression.

NULLIF function returns the first expression if the two expressions are not equivalent. Otherwise, it returns a NULL value.

The following sample code retrieves the average StandardCost products belonging to the class 'L'.


SET NOCOUNT ON USE
Adventureworks ;
SELECT AVG ( StandardCost ) AvgCost
AS FROM Production. Product
WHERE Class = 'L'

The following is the full result set the query. Average cost

---------------

220.4349

This average includes some rows that have NULL values \u200b\u200bin column StandardCost. Therefore, the value shown in the result set is incorrect average cost. However, the same query can be rewritten with the help of the NULLIF function to ensure that products StandardCost NULL are not included in the calculation. USE

   Adventureworks ; 
SELECT AVG (NULLIF ( StandardCost , 0)) AS AvgCost
FROM Production. WHERE
Product Class = 'L'


The following is the full result set the query.



AvgCost

---------------


240.2493






  • COALESCE function returns the first expression not null among its arguments. The following is the syntax of the merge function:



COALESCE (expression [, ... N])



The function takes two arguments. The expression argument is an expression of any data type. The argument n is a placeholder indicating that multiple expressions can be specified. The same type of data should all expressions.



COALESCE function returns the result the same data type as the expression. If all arguments of the merge function is NULL, then returns NULL.



The AdventureWorks database, consider the following table containing the rows CustomerPhone































































Customer_Name Home_Phone Office_Phone Mobile_Phone

Thomas



0225872342



NULL



9870414716



Gary



NULL



NULL



982034452



Janet



NULL



099345522



NULL



Steve



232323452



334435454



NULL



Michelle



NULL



NULL



NULL




The above table contains multiple records for a particular client. Some clients do not have all the phone numbers of three. The following code sample uses the combination to return the phone number is not null first, for a particular client and for clients with no valid phone numbers, the chain of 'non-registered number "is displayed.




 SET NOCOUNT OFF  
USE AdventureWorks SELECT
Customer_Name , COALESCE ( Mobile_Phone , Office_Phone , Home_Phone , 'No Listed Number' ) as Telephonefrom CustomerPhone



The following is the full result set of query.


The Following is the complete result set of the query.








































Customer_Name Telephone

Thomas



9870414716



Gary



982034452



Janet



099345522



Steve



334435454



Michelle



Number Not Listed


0 comments:

Post a Comment