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.