Here is some values from a specific column in SQL Server:
1899-12-30 00:00:00.0000000
1899-12-30 00:00:00.0000000
2022-03-01 00:00:00.0000000
2022-03-01 00:00:00.0000000
Column type is datetime2(7)
What is the correct way of creating an insert statement to insert a specific date?
Is this method okay?:
insert into table1 (date) values ('2020/01/31')
Is this acceptable? Or should it be:
insert into table1 (date) values ('2020/01/31 00:00:00.0000000')
?
Using SQL Server
You can use standard date format YYYY-MM-DD
:
insert into table1 (date)
values ('2020-01-31')
For all versions of SQL Server (up until this point in time, SQL Server 2019) the following string format is the "standard" way of inserting string dates into date columns in SQL Server.
INSERT INTO TABLE (DateColumn) VALUES ('20200131');
Where the 20200131
value corresponds to 2020 01 31 (year, month, date without spaces).
Both are acceptable, although the syntax for datetime2 is YYYY-MM-DDThh:mm:ss[.nnnnnnn]
.
DATE only The TIME part defaults to 00:00:00.
TIME only The DATE part defaults to 1900-1-1.
DATE + TIME Trivial
DATE + TIMEZONE Not allowed.
DATE + TIME + TIMEZONE The local DATETIME will be used.
we can use BOTH
INSERT INTO MyTable
(Date)
VALUES
('2022-03-01')
GO
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.