Use ISNULL in SQL Server to execute null value judgment query, serverisnull

Source: Internet
Author: User
Tags sql server query

Use ISNULL in SQL Server to execute null value judgment query, serverisnull

The following queries are available:
Copy codeThe Code is as follows:
Select isnull (lastchanged, '') as lastchanged from vhhostlist where s_comment = 'test202'

Originally, the ISNULL function has only one parameter. It indicates that the parameter is used to determine whether the value of this parameter is NULL. If it is NULL, TRUE is returned; otherwise, FALSE is returned;

However, in the SQL Server query statement, isnull requires two parameters, which means that if parameter 1 is NULL, parameter 2 is the return value of the isnull function;

That is, the meaning of the preceding query is to query the value of the lastchanged field of s_comment = 'test202 'in the vhhostlist table. If it is NULL, an empty string is returned;

I remember that when I didn't understand this before, I encountered the following situation: one order table, one of which is status, and the status is NULL when the order is submitted. Make the following judgment in the program. When it is NULL, a review hyperlink is displayed; when it is not empty, a string is displayed: reviewed. At that time, the program was written as follows:
Copy codeThe Code is as follows:
If rs ("status") <> "then
Response. write "<span style = 'color: red; '> reviewed </span>"
Else
Response. write "<a href = check. asp? Id = "& rs (" id ") &"> review </a>"
End if

In fact, this judgment is not accurate. rs ("status") <> "does not mean that rs (" status ") is already reviewed; it does not indicate that the rs ("status") <> "is not audited;

The most reasonable one is to use isnull (status, '') as status to check the status. If rs (" status ") =" ", it indicates that the status is not reviewed. This must be true!


How can I determine whether a queried value is null in SQL server?

You cannot get the correct value because the result field has null.

This way
Select SUM (isnull (Result, 0) as Result from PDS_LabResultData
The isnull function is used.
Isnull (Result, 0) indicates that if result is null, the value is 0.


How can I judge that fields in the SQL SERVER table are empty?

In SQL server, 12:00:00 AM is also considered null ('', not null)

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.