SQL Server processing summary with time Blank

Source: Internet
Author: User
In SQL Server, fields of the DateTime type are often used. When the field value of this type is null, various exceptions often occur.

DateTime fields are often used in SQL server. When the field value of this type is null, various exceptions often occur.

We will summarize several main cases:
I. How to enter a NULL value
If you do not enter a null value, if the time is null, "" is written by default, which is troublesome for business processing.
Ctrl + 0 to enter a NULL value.
Ii. How to judge the time field as NULL in an SQL statement
Assume that the table is TestTable.
SN DateTime1 DateTime2
1 2011-10-24 2011-10-25
2 NULL 2011-10-26
3 2011-10-25 NULL
Use case for query. If it is written:
Select (case DateTime1 when NULL then 'a 'else' end) from TestTable
The query result is:

B

This is obviously not the expected result; you need to write it:
Select (case DateTime1 when DateTime1 then 'B' else' end) from TestTable
The query result is:

A

This is the expected result.

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.