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

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.

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.





13 Comments. Leave new
In MySQL its binary(0)
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
nice to know, but not really a question for an interview
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.
Not a good question for an interview.
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.
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) ?
Comes in handy when explaining datatype errors in UNION (ALL) queries
It doesn’t matter whether a question for interview or not, nice to know the fact, that matters.
good explanations
Thanks Sadhu.
SELECT * FROM sys.dm_exec_describe_first_result_set(‘
SELECT NULL AS col
‘, NULL, 0)
So how do you control the datatype of NULL column when using SELECT INTO TABLE statement?