Examples of SQL Server IsNull applications

Source: Internet
Author: User
Tags expression join sql server isnull connectionstrings
Server|sqlserver

ISNULL
Replaces NULL with the specified replacement value.

Grammar
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

An expression to be returned when check_expression is null. Replacement_value must have the same type as check_expresssion.

return type
Returns the same type as check_expression.

Comments
Returns the value of the expression if check_expression is not NULL, otherwise return replacement_value.

Example
A. Use ISNULL with AVG
The following example finds the average price of all books and replaces all NULL entries in the prices column of the titles table with the value $10.00.

Use pubs
Go
SELECT AVG (ISNULL (Price, $10.00))
From titles
Go

Here is the result set:

--------------------------
14.24

(1 row (s) affected)

B. Use of ISNULL
The following example selects the title, type, and price for all the books in the titles table. If the price of a title is NULL, the price shown in the result set is 0.00.

Use pubs
Go
SELECT SUBSTRING (title, 1,) as title, type as type,
ISNULL (Price, 0.00) as Price
From titles
Go

C. Using IsNull in full join
Table A:
Tid
Uid
Anum

Table B:
Tbid
Uid
Bnum1
Bnum2
Two tables need to be fully connected via UID:
Select A.tid,a.uid,a.anum,b.bnum1,b.bnum2 from a full join B on a.uid=b.uid
Full connection will have a lot of empty cases, you can use IsNull to resolve, instead:
Select IsNull (A.tid,b.tid), IsNull (A.uid,b.uid), IsNull (a.anum,0), IsNull (b.bnum1,0), IsNull (b.bnum2,0) from a full Join B on A.uid=b.uid

Plus a comprehensive usage in asp.net2.0:

<%@ Page language= "C #"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.