Sqlserver isnull function and SQL statement to judge whether the value is empty

Time:2022-5-14

First, it’s useful

Copy codeThe code is as follows:
Use database
update news set author=’jb51′ where author is null

If your is incorrect, it means you have the wrong number. Take a closer look. It is strongly recommended to back up the database before operation.

Description: replaces null with the specified replacement value.

Syntax: isnull (check_expression, replacement_value)

Parameters:

check_ Expression: an expression that will be checked for null. check_ Expression can be of any type.

replacement_ Value: when check_ The expression to return when expression is null. replacement_ Value must be a value that can be implicitly converted to check_ Type of expression type.

Return value: return and check_ Expression is the same type.

Note: if check_ If expression is not null, its value is returned; Otherwise, replace_ Value is implicitly converted to check_ After the type of expression (if the two types are different), the former is returned.

example:

Copy codeThe code is as follows:
SELECT AVG(ISNULL(Weight, 50))
FROM Production.Product;

Discrimination:

Do not use isnull to find null values. Instead, use is null. The following example finds all products with null in the weight column. Notice 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

Determine whether the variable is null:

IF (@VaribleName IS NULL)

Select records with null field value:

WHERE COLUMN_NAME IS NULL

Isnull() function:

ISNULL(@VaribleName, ‘DefaultValue’)
ISNULL(COLUMN_NAME, ‘Default Value’)

Recommended Today

Analysis of Django interface version control

catalogue 1、 Foreword 2、 Disposition 2.1. Global configuration 2.2. View configuration 3、 Five version control classes built in DRF 3.1、AcceptHeaderVersioning 3.1.1. HTTP access method 3.1.2、settings 3.1.3、serializers 3.1.4、views 3.1.5 access 3.2、URLPathVersioning 3.2.1. HTTP access method 3.2.2、settings 3.2.3、urls 3.2.4 access 3.3、NamespaceVersioning 3.3.1. HTTP access method 3.3.2、settings 3.3.3、urls 3.3.4 access 1、 Foreword stayRESTfulIn the specification, for problems related […]