Useful first
Copy codeThe Code is as follows:
Use Database
Update news set author = 'jb51 'where author is null
If you are incorrect, it means that you have made a mistake. Taking a closer look, we strongly recommend that you back up the database before performing operations.
Note: replace NULL with the specified replacement value.
Syntax: ISNULL (check_expression, replacement_value)
Parameters:
Check_expression: The expression to be checked for NULL. Check_expression can be of any type.
Replacement_value: The expression to be returned when check_expression is NULL. Replacement_value must be a type that can be implicitly converted to the check_expresssion type.
Return Value: returns the same type as check_expression.
NOTE: If check_expression is not NULL, its value is returned. Otherwise, the former is returned after the replacement_value is implicitly converted to the check_expression type (if the two types are different.
Instance:
Copy codeThe Code is as follows:
Select avg (ISNULL (Weight, 50 ))
FROM Production. Product;
Analysis:
Do not use ISNULL to find NULL values. Instead, use is null. The following example finds all products with NULL in the weight column. Note the space between IS and NULL.
Copy codeThe Code is as follows:
USE AdventureWorks2008R2;
GO
SELECT Name, Weight
FROM Production. Product
WHERE Weight is null;
GO
SQL Server: how to determine whether a variable or field is NULL
Judge whether the variable is NULL:
IF (@ VaribleName is null)
Select a record whose field value is NULL:
WHERE COLUMN_NAME IS NULL
ISNULL () function:
ISNULL (@ VaribleName, 'defaultvalue ')
ISNULL (COLUMN_NAME, 'default value ')