Languages
[Edit]
EN

MS SQL Server - Insert NULL values

0 points
Created by:
Roseanne-Read
1335

In this article, we would like to show you how to insert NULL values to the table in MS SQL Server.

Quick solution:

INSERT INTO [table_name]
    ([column1], [column2], [column3], [columnN])
VALUES
    (value1, NULL, NULL, valueN);

Practical example

To show you how to insert NULL values to the table, we will use the following users table:

MS SQL Server - example data used to insert NULL values to the table
MS SQL Server - example data used to insert NULL values to the table

Note:

At the end of this article you can find database preparation SQL queries.

Example

In this example, we will insert rows with NULL values to the users table.

Query

INSERT INTO [users]
	( [name], [surname], [department_id], [salary])
VALUES
	(NULL, NULL, NULL, NULL),
	('Simon', NULL, NULL, NULL),
	('Taylor', 'Martin', NULL, NULL),
	('Andrew', 'Thompson', 1, NULL),
	('Taylor', 'Martin', 2, '2000');

Result:

MS SQL Server - insert NULL values to the table - result
MS SQL Server - insert NULL values to the table - result

Database preparation

create_tables.sql file:

CREATE TABLE [users] (
	[id] INT IDENTITY(1,1),
	[name] VARCHAR(50),
	[surname] VARCHAR(50),
	[department_id] INT,
    [salary] DECIMAL(15,2),
	PRIMARY KEY ([id])
);

insert_data.sql file:

INSERT INTO [users]
	( [name], [surname], [department_id], [salary])
VALUES
	('John', 'Stewart', 1, '6000'),
	('Chris', 'Brown', 2, '6000'),
	('Kate', 'Lewis', 3, '4000');
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MS SQL Server - problems

MS SQL Server - Insert NULL values
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join