If the field is fixed width storing NULL takes the same space as any other value - the width of the field.

If the field is variable width the NULL value takes up no space.

In addition to the space required to store a null value there is also an overhead for having a nullable column. For each row one bit is used per nullable column to mark whether the value for that column is null or not. This is true whether the column is fixed or variable length.


The reason for the discrepancies that you have observed in information from other sources:

  • The start of the first article is a bit misleading. The article is not talking about the cost of storing a NULL value, but the cost of having the ability to store a NULL (i.e the cost of making a column nullable). It's true that it costs something in storage space to make a column nullable, but once you have done that it takes less space to store a NULL than it takes to store a value (for variable width columns).

  • The second link seems to be a question about Microsoft Access. I don't know the details of how Access stores NULLs but I wouldn't be surprised if it is different to SQL Server.

Answer from Mark Byers on Stack Overflow
Top answer
1 of 6
170

If the field is fixed width storing NULL takes the same space as any other value - the width of the field.

If the field is variable width the NULL value takes up no space.

In addition to the space required to store a null value there is also an overhead for having a nullable column. For each row one bit is used per nullable column to mark whether the value for that column is null or not. This is true whether the column is fixed or variable length.


The reason for the discrepancies that you have observed in information from other sources:

  • The start of the first article is a bit misleading. The article is not talking about the cost of storing a NULL value, but the cost of having the ability to store a NULL (i.e the cost of making a column nullable). It's true that it costs something in storage space to make a column nullable, but once you have done that it takes less space to store a NULL than it takes to store a value (for variable width columns).

  • The second link seems to be a question about Microsoft Access. I don't know the details of how Access stores NULLs but I wouldn't be surprised if it is different to SQL Server.

2 of 6
32

The following link claims that if the column is variable length, i.e. varchar then NULL takes 0 bytes (plus 1 byte is used to flag whether value is NULL or not):

  • How does SQL Server really store NULL-s

The above link, as well as the below link, claim that for fixed length columns, i.e. char(10) or int, a value of NULL occupies the length of the column (plus 1 byte to flag whether it's NULL or not):

  • Data Type Performance Tuning Tips for Microsoft SQL Server

Examples:

  1. If you set a char(10) to NULL, it occupies 10 bytes (zeroed out)
  2. An int takes 4 bytes (also zeroed out).
  3. A varchar(1 million) set to NULL takes 0 bytes (+ 2 bytes)

Note: on a slight tangent, the storage size of varchar is the length of data entered + 2 bytes.

🌐
Quora
quora.com › What-is-size-of-null-in-c-language
What is size of null in c language? - Quora
Answer (1 of 4): A good answer from stackOverflow Question - why sizeof(“”) is equivalent to 1 and sizeof(NULL) is equivalent to 4 in c-language? Ans - A string literal is an array of characters* (with static storage), which contains all ...
Discussions

elementary set theory - What's the difference between a null set and an empty set? - Mathematics Stack Exchange
From my notes, I understand that an empty set always has a null size (is this different from saying that it has a size = 0?). What are then examples of non-empty null sets? Thanks for your time. ... More on math.stackexchange.com
🌐 math.stackexchange.com
Sizeof NULL value of uniqueidentifier
Will be the size 10*16 B, or (10 - count of NULL columns) * 16 B? Maybe it would be better having rather columns of type varchar(32). ... A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions. ... To reduce the space, i think that it is ... More on learn.microsoft.com
🌐 learn.microsoft.com
4
0
Understanding difference between 0 and NULL
NULL is a macro that expands to a null pointer constant. 0 is a null pointer constant. That means NULL could just be defined as 0. Put simply, sizeof (NULL) is simply not something you can rely on as having a consistent, implementation-independent meaning — you don't even know what type NULL has. It is entirely possible for sizeof (NULL) to be not equal to sizeof (void *). If you want to know how big a particular pointer type is, use sizeof on that pointer type, or on a value of that pointer type. More on reddit.com
🌐 r/C_Programming
36
15
July 2, 2023
SIZE OF NULL COLUMN – SQLServerCentral Forums
SIZE OF NULL COLUMN Forum – Learn more on SQLServerCentral More on sqlservercentral.com
🌐 sqlservercentral.com
July 8, 2015
Top answer
1 of 5
21

A string literal is an array of characters* (with static storage), which contains all the characters in the literal along with a terminator. The size of an array is the size of the element multiplied by the number of elements in the array.

The literal "" is an array that consists of one char with the value 0. The type is char[1], and sizeof(char) is always one; thereforesizeof(char[1]) is always one.

In C, NULL is implementation-defined, and is often ((void*)0). The size of a void*, on your particular implementation, is 4. It may be a different number depending on the platform you run on. NULL may also expand to an integer of some type of the value 0, and you'd get the size of that instead.

*A literal is not a pointer, arrays are not pointers, pointers do not play a role in this part of the question.

2 of 5
15

The empty string "" has type char[1], or "array 1 of char". It is not a pointer, as most people believe. It can decay into a pointer, so any time a pointer to char is expected, you can use an array of char instead, and the array will decay into a pointer to its first element.

Since sizeof(char) is 1 (by definition), we therefore have sizeof("") is sizeof(char[1]), which is 1*1 = 1.

In C, NULL is an "implementation-defined null pointer constant" (C99 §7.17.3). A "null pointer constant" is defined to be an integer expression with the value 0, or such an expression cast to type void * (C99 §6.3.2.3.3). So the actual value of sizeof(NULL) is implementation-defined: you might get sizeof(int), or you might get sizeof(void*). On 64-bit systems, you often have sizeof(int) == 4 and sizeof(void*) == 8, which means you can't depend on what sizeof(NULL) is.

Also note that most C implementations define NULL as ((void*)0) (though this is not required by the standard), whereas most C++ implementations just define NULL as a plain 0. This means that the value of sizeof(NULL) can and will change depending on if code is compiled as C or as C++ (for example, code in header files shared between C and C++ source files). So do not depend on sizeof(NULL).

🌐
Quora
quora.com › What-is-the-size-of-a-null-string
What is the size of a null string? - Quora
Answer (1 of 4): It depends on what a “NULL string” means in various languages. * In a language like C, you can have a char pointer that is set to NULL. In this case, the “string part” isn’t represented, and the only space used is the space for the char pointer itself.
🌐
DiKnows Tech
diknowstech.wordpress.com › 2009 › 12 › 21 › what-is-the-size-of-null-operator-in-java
What is the Size of ” null ” Operator in Java? | DiKnows Tech
January 2, 2010 - from the heap memory perspective it will be 0, as it wont allocate any memory from heap. But from stack memory perspective the object has reference which is set to null. and the reference variable size depends your system.
🌐
Sololearn
sololearn.com › en › Discuss › 3027137 › what-is-the-size-of-null-pointer
What is the size of null pointer?? | Sololearn: Learn to code for FREE!
sizeof("") is equivalent to 1 .and sizeof(NULL) is equivalent to 4 in c · 3rd May 2022, 11:14 AM · 🤔☺️😳😇 · Answer · Learn more efficiently, for free: Introduction to Python · 7.1M learners · Introduction to Java · 4.7M learners ...
Find elsewhere
Top answer
1 of 4
2

Hi @Jan Vávra
Each row has a null bitmap for columns that allow nulls. If the row in that column is null then a bit in the bitmap is 1 else it's 0.
For variable size datatypes the acctual size is 0 bytes.
For fixed size datatype the acctual size is the default datatype size in bytes set to default value (0 for numbers, '' for chars).
In my opinion, uniqueidentifier is a fixed size datatype.
Check this sample:

declare @guid1 uniqueidentifier = null  
       ,@guid2 uniqueidentifier = '728B7419-6E97-4F54-B06B-536B31AE954E'  
	   ,@guid3 uniqueidentifier = '728B7419-6E97-4F54-B06B-536B31AE954E4578RT'  
select DATALENGTH(@guid1)  
      ,DATALENGTH(@guid2)  
	  ,DATALENGTH(@guid3)  

Best regards,
LiHong

2 of 4
1

As Li says, each value will take up 16 bytes, no matter if it's NULL or not. However, there is a twist: if you apply row compression, the NULL values will not take up any bytes beside the bit in the NULL bitmap.

If row compression is not an alternative to you for some reason, varbinary(16) may be a better alternative. I will have to admit that I cannot really decide what I think in this case. I would probably consider the complexity of programming. One thing to keep in mind is that if index these columns and you do:

DECLARE @v uniqueidentifier  
...  
SELECT ... FROM tbl WHERE indexedbincol = @v  

The rules of type precedence in SQL Server will convert the binary column to uniqueidentifier, which most likely will prevent an Index Seek. (I have not actually tested to verify.) So if you go for varbinary(16), you should so throughout your application.

Viorel is perfectly right in that varchar(32) is not an alternative.

🌐
Wikipedia
en.wikipedia.org › wiki › Null_character
Null character - Wikipedia
March 3, 2026 - This design allows a string to be any length at the cost of only one extra character of memory. The common competing design for a string stores the length of the string as an integer data type, but this limits the size of the string to the range ...
🌐
C For Dummies
c-for-dummies.com › blog
The Difference Between NULL and Zero | C For Dummies Blog
The int value \0 is 4 bytes long (on my computer), which helps confirm it’s an int. But the NULL value is 8 bytes long, which is the size of an address.
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › size-of-null-column
SIZE OF NULL COLUMN – SQLServerCentral Forums
July 8, 2015 - i explain all with exmple it is the best 🙂 please do int on qa not in prod. (its not big data but you know 🙂 ... Yes null columns take up space, you can potentially however reduce this overhead using sparse columns:
🌐
SQLServerCentral
sqlservercentral.com › home › topics › sql server 2016 › sql server 2016 - development and t-sql › null storage
NULL Storage – SQLServerCentral Forums
April 30, 2019 - And if NULL is set as 1 bit per column, then I should be showing the size of the table with the NULL column as 100,000/8 bytes larger just for that bit.
🌐
Creative COW
creativecow.net › forums › adobe after effects › null object / layer size?
Null Object / Layer Size? - Adobe After Effects - Creative COW
In Cinema4D you can have nulls be all kinds of sizes and shapes regardless of their scale. Remember, folks, the more people who file the same feature request, the more likely that feature will be to be implemented. – The Great Szalam (The ‘Great’ stands for ‘Not So Great, in fact, Extremely Humble’) No trees were harmed in the creation of this message, but several thousand electrons were mildly inconvenienced. ... Yeah – don’t think there is anything you can do about the null size besides un-parenting, scaling the null, then re-parenting – but I’m not sure if that would even be worth it.
🌐
Medium
medium.com › @mndpsngh21 › unlocking-database-efficiency-how-null-saves-storage-space-in-mysql-and-postgresql-527b84a0715d
Unlocking Database Efficiency: How “Null” Saves Storage Space in MySQL and PostgreSQL | by mandeep singh | Medium
September 24, 2023 - In databases, “null” is a special value that signifies the absence of data in a specific field or column. It’s not the same as an empty string or zero; instead, it indicates that no data is available or defined for that particular field.
Top answer
1 of 6
49

In your example myApple has the special value null (typically all zero bits), and so is referencing nothing. The object that it originally referred to is now lost on the heap. There is no way to retrieve its location. This is known as a memory leak on systems without garbage collection.

If you originally set 1000 references to null, then you have space for just 1000 references, typically 1000 * 4 bytes (on a 32-bit system, twice that on 64). If those 1000 references originally pointed to real objects, then you allocated 1000 times the size of each object, plus space for the 1000 references.

In some languages (like C and C++), pointers always point to something, even when "uninitialized". The issue is whether the address they hold is legal for your program to access. The special address zero (aka null) is deliberately not mapped into your address space, so a segmentation fault is generated by the memory management unit (MMU) when it is accessed and your program crashes. But since address zero is deliberately not mapped in, it becomes an ideal value to use to indicate that a pointer is not pointing to anything, hence its role as null. To complete the story, as you allocate memory with new or malloc(), the operating system configures the MMU to map pages of RAM into your address space and they become usable. There are still typically vast ranges of address space that are not mapped in, and so lead to segmentation faults, too.

2 of 6
12

The answer depends on the language you're using.

C/C++

In C and C++, the keyword was NULL, and what NULL really was was 0. It was decided that "0x0000" was never going to be a valid pointer to an object, and so that is the value which gets assigned to indicate that it is not a valid pointer. However, it's completely arbitrary. If you attempted to access it like a pointer, it would behave exactly like a pointer to an object which no longer exists in memory, causing a invalid pointer exception to be thrown. The pointer itself occupies memory, but no more than an integer object would. Hence, if you have 1000 null pointers, it is the equivalent of 1000 integers. If some of those pointers point to valid objects, then the usage of memory would be the equivalent of 1000 integers plus the memory contained in those valid pointers. Remember that in C or C++, if a pointer no longer points to its object, that does not imply memory has been released, so you must explicitly delete that object using dealloc (C) or delete (C++).

Java

Unlike in C and C++, in Java null is merely a keyword. Rather than managing null like a pointer to an object, it is managed internally and treated like a literal. This eliminated the need to tie in pointers as integer types and allows Java to abstract away pointers entirely. However even if Java hides it better, they are still pointers, meaning 1000 null pointers still consume the equivalent of 1000 integers. Obviously when they point to objects, much like C and C++, memory is consumed by those objects until no more pointers reference them, however unlike in C and C++, the garbage collector picks up on it on its next pass and frees up the memory, without requiring that you have to keep track of what objects are freed up and which objects are not, in most cases (unless you have reasons to weakly reference objects for example).

🌐
Quora
quora.com › What-is-the-difference-between-being-null-or-having-a-size-of-zero-in-a-list
What is the difference between being null or having a size of zero in a list? - Quora
Answer (1 of 37): I'll assume this is Java, but the answer should be about the same in any case. For that example, you will get the same behavior, because that method guards against nulls. But in other cases, there is a difference between a null and an empty list. The most notable is that if y...
🌐
GitHub
github.com › dwyl › learn-postgresql › issues › 49
How much space do null values take up in PostgreSQL? · Issue #49 · dwyl/learn-postgresql
November 15, 2018 - One of the common questions when using PostgreSQL is around having "empty" fields in a table. Imagine that you have a schema for a person (a human being that uses your SaaS product). That person might have a wide range of data associated with them. see: dwyl/fields#4 Let's consider a typical record: ... As you can see from this basic "person" record, Alex does not have an instagram or facebook account. Both columns in the table will be null:
Author   dwyl
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › null-character
Null Character - an overview | ScienceDirect Topics
The null character is used in the C programming language to mark the end of a string, requiring the size of the string array to be at least one byte larger than the string itself to accommodate this final character. 4 When copying strings in C, the process stops at the null character, which ...