Quantcast
Channel: Smart ERP Solutions - Microsoft BI
Viewing all articles
Browse latest Browse all 22

NULL Behavior

$
0
0

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_IDProductNameUnitsOnOrder

1

CoverdWire

15

2

OpticalWire 

3

TeliphoneWire

20

4

LanWire

21

5

Wire 

T-SQL script to generate above table with data is:

fig22

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.

fig23

Result:

 SUM_UnitsOnOrderCOUNT_UnitsOnOrderAVG_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:

fig24

 Val_SUMVal_MultVal_Avg
1NULLNULLNULL

Here we can see that any arithmetic operation with NULL always return NULL.

Similarly, the concatenation of string value with NULL always return NULL.

fig 24 - Result: NULL

2. Comparison of NULL with NULL always result nothing. Ex:

fig 25 - 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:

fig27  fig26

Result:

 P_IDProductNameUnitsOnOrder
    
 P_IDProductNameUnitsOnOrder
1

2

OpticalWireNULL
2

5

WireNULL

 

 

 

Let us see another example:

fig28   fig29

To counter above observation (to compare NULL with NULL without setting ANSI_NULL off), T-SQL uses ISNULL keyword.

ex: fig30 - Result; 6

fig31 >

 P_IDProductNameUnitsOnOrder
1

2

OpticalWireNULL
2

5

WireNULL

 

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

fig32 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

fig33

 RVERSIONTSTAMPINTVCHAR

1

0x0xNULLNULL

 

 

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:

fig34

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.

fig35

 Column_nameTypeComputedLengthPrecScaleNullable

1

COL1intno

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.

fig36

 Column_nameTypeComputedLengthPrecScaleNullable

1

COL1datetimeno

4

10

0

yes

 

 

By: Sumit Kumar Singh


Viewing all articles
Browse latest Browse all 22

Trending Articles