What is the Default Datatype of NULL? – Interview Question of the Week #131

Interview question: What is the default data type of an untyped NULL in SQL Server?

Shapeless clay takes the form of the selected square mold

Answer: NULL itself is a marker for an absent value, not a universal data type. But SELECT NULL AS col INTO #TempTable creates an int column, because SQL Server must choose a concrete type for the new table. The answer is int for that experiment, not “every NULL is an int.”

When I ask this question, many DBAs and developers tell me NULL has no data type. That’s a fair answer until you notice the word default in my question. I want to see what SQL Server infers when it has no type information to inherit.

Here is the test, with a catalog query that makes the resulting column type easy to inspect:

IF OBJECT_ID(N'tempdb..#NullType') IS NOT NULL
    THROW 50135, 'Demo temporary table exists. Open a fresh query window.', 1;
SELECT NULL AS col
INTO #NullType;
SELECT c.name AS column_name,
       TYPE_NAME(c.user_type_id) AS inferred_type
FROM tempdb.sys.columns AS c
WHERE c.object_id = OBJECT_ID(N'tempdb..#NullType');
DROP TABLE #NullType;

The inferred_type is int. Now make the intended type explicit:

IF OBJECT_ID(N'tempdb..#TypedNull') IS NOT NULL
    THROW 50135, 'Demo temporary table exists. Open a fresh query window.', 1;
SELECT CAST(NULL AS varchar(20)) AS col
INTO #TypedNull;
SELECT c.name AS column_name,
       TYPE_NAME(c.user_type_id) AS inferred_type
FROM tempdb.sys.columns AS c
WHERE c.object_id = OBJECT_ID(N'tempdb..#TypedNull');
DROP TABLE #TypedNull;

This time the new column is varchar. A SELECT INTO column follows the type of its select-list expression. SQL Server may infer a different type when a surrounding expression or target column supplies context, so use an explicit CAST when a result contract matters.

The useful takeaway is the distinction between an absent value and the concrete type SQL Server assigns to an expression in a specific context.

Untyped NULL: What type is NULL?

NULL is not a data type, it is a missing value, and SELECT INTO still has to give its column one.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Datatype, SQL Server
Previous Post
What does BACKUP WITH NAME Command do? – Interview Question of the Week #130
Next Post
How to Count Week Days Between Two Dates? – Interview Question of the Week #132

Related Posts

13 Comments. Leave new

  • In MySQL its binary(0)

    Reply
  • Peter Davies
    July 17, 2017 7:02 pm

    Its more economical to have a default int which is fixed known number of bytes. I don’t find it particularly interesting myself as an interview question. What does it prove about the person and how would we utilize this in the application of any DBA work.
    BY the way liked your Group By video , now that was interesting

    Reply
  • wilfred van Dijk
    July 17, 2017 7:43 pm

    nice to know, but not really a question for an interview

    Reply
    • Morgan Smith
      July 12, 2024 1:04 am

      Not a great question for an Admin position but great question for a Developer position. This question helps when writing UNION (ALL) queries and getting datatype errors because of selecting a NULL for a column.

      This blog has come in handy when explaining these types of datatype errors.

      Reply
  • Not a good question for an interview.

    Reply
  • Super Cricket
    July 17, 2017 10:48 pm

    He he… It is indeed fun to see so many says not a good question for interview.

    Please note that you are allowed to NOT ask this question in interview. This questions are for beginners and not for all of you who knows everything.

    There are many people like me who learned this today.

    Reply
  • Ok, but what is the practical implication of this ? what does it matter whether the default is int or varchar (or any other datatype for this matter) ?

    Reply
  • It doesn’t matter whether a question for interview or not, nice to know the fact, that matters.

    Reply
  • good explanations

    Reply
  • SELECT * FROM sys.dm_exec_describe_first_result_set(‘
    SELECT NULL AS col
    ‘, NULL, 0)

    Reply
  • So how do you control the datatype of NULL column when using SELECT INTO TABLE statement?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.