Clik here to view.

NULL in T-SQL
NULL
is contagious: use it in a numerical, string or date/time operation, and the result will invariably be NULL
. With Boolean operators, the outcome depends on the type of operation and the value of the other operand.
Consider following table loaded with product sales data as:
P_ID | ProductName | UnitsOnOrder |
1 | CoverdWire | 15 |
2 | OpticalWire | |
3 | TeliphoneWire | 20 |
4 | LanWire | 21 |
5 | Wire |
T-SQL script to generate above table with data is:
Image may be NSFW.
Clik here to view.
Mathematical and string operations
Now perform following operation to understand the nature of “NULL”:
1. Perform aggregate operation on the column “UnitsOnOrder”. This column has couple of NULL values also.
Image may be NSFW.
Clik here to view.
Result:
SUM_UnitsOnOrder | COUNT_UnitsOnOrder | AVG_UnitsOnOrder | |
1 | 56 | 3 | 18.666666 |
Here we can observe that NULL values by default omitted in all the aggregate functions.
But, if the NULL value is present in any arithmetic operation explicitly, then the NULL value cannot be ignored and it affects the result completely. See below:
Image may be NSFW.
Clik here to view.
Val_SUM | Val_Mult | Val_Avg | |
1 | NULL | NULL | NULL |
Here we can see that any arithmetic operation with NULL always return NULL.
Similarly, the concatenation of string value with NULL always return NULL.
Image may be NSFW.
Clik here to view. - Result: NULL
2. Comparison of NULL with NULL always result nothing. Ex:
Image may be NSFW.
Clik here to view. - Result <no records>
As NULL is defined as absence of value, undefined, or the value which is unknown at this point of time, so an unknown cannot be compared with another unknown.
NOTE: The above observation holds true only when we have SETANSI_NULLSON. If SET ANSI_NULLS is OFF, thenNULL comparison with NULL gives desired result.
Ex:
Image may be NSFW.
Clik here to view. Image may be NSFW.
Clik here to view.
Result:
P_ID | ProductName | UnitsOnOrder | |
P_ID | ProductName | UnitsOnOrder | |
1 | 2 | OpticalWire | NULL |
2 | 5 | Wire | NULL |
Let us see another example:
Image may be NSFW.
Clik here to view. Image may be NSFW.
Clik here to view.
To counter above observation (to compare NULL with NULL without setting ANSI_NULL off), T-SQL uses ISNULL keyword.
ex: Image may be NSFW.
Clik here to view. - Result; 6
Image may be NSFW.
Clik here to view. >
P_ID | ProductName | UnitsOnOrder | |
1 | 2 | OpticalWire | NULL |
2 | 5 | Wire | NULL |
Similarly, behavior of NULL holds true for <> NULLalso. Any T-SQL code with <> NULL always return NULL. To counter this behavior, either we need to SET ANSI_NULL OFF or use IS NOT NULL keyword.
Boolean operations
Consider we have one entities A and it isNULL. Now let’s see behavior of NULL in Boolean operations:
- If A is unknown, it’s inverse is also unknown.
- “A or false” always has the same value as A
- “A or true” is always true. A’s value doesn’t matter.
- “A or A” always equals A – which is NULL.
- “A and false” is always false – A’s value doesn’t matter.
- “A and true” always has the same value as A – which is unknown.
- “A and A” always equals A – which is NULL.
Some other observation about NULL values are:
1. By default NULL values comes first when a column is in ascending order and comes last when in descending order
Image may be NSFW.
Clik here to view. Result:
UnitsOnOrders |
NULL |
NULL |
15 |
20 |
21 |
2. The only data types that will interpret NULL differently are ‘ROWVERSION’and‘TIMESTAMP’.
Run this code and see the output
Image may be NSFW.
Clik here to view.
RVERSION | TSTAMP | INT | VCHAR | |
1 | 0x | 0x | NULL | NULL |
3. As we know, NULL is the absence of data and any datatype can be NULL. But by default a NULL value is considered to be of INT datatype.
Let us run the following code:
Image may be NSFW.
Clik here to view.
Here, a NULL value is copied into a temporary table #Temp_Table. Now run the following code to see the table structure using sp_help or by using Alt+F1 key.
Image may be NSFW.
Clik here to view.
Column_name | Type | Computed | Length | Prec | Scale | Nullable | |
1 | COL1 | int | no | 4 | 10 | 0 | yes |
Here, we can see that by default NULL (COL1) has INT datatype.
If you want it to be specific datatype, you need to explicitly convert it to different data type as shown in the below code.
Image may be NSFW.
Clik here to view.
Column_name | Type | Computed | Length | Prec | Scale | Nullable | |
1 | COL1 | datetime | no | 4 | 10 | 0 | yes |
By: Sumit Kumar Singh