首页 > 解决方案 > 使用日期格式将值简单地插入表中

问题描述

我正在尝试将一些基本数据插入表中,但我不断收到错误消息。起初,我以为我只是在插入日期列时的日期格式不正确,但现在我在猜测。

最初,第 4 列的日期格式为 12-Jan-10 的数据类型DATE。我已将格式更改为 2010-01-12 但我仍然收到相同的错误。我可能在这里遗漏了一些简单的东西,但我不太确定它是什么。

我得到的错误是

消息 241,级别 16,状态 1,第 2 行
从字符串转换日期和/或时间时转换失败。

这是表格的列...

BookName (PK, Varchar(45), not null)
Genre (varchar(25), not null)
DateOfPublication (date,not null)
NoOfPages (int, not null)
WriterID (PK, FK, varchar(11), not null)
EditorID (FK, varchar(11), not null)
insert into BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('Valley of Heroes','10','Fiction','2010-01-12',874,'20');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('The Ruler''s Return','11','Fantasy','2012-03-14',765,'22');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('eRobot','11','Fantasy','2011-04-15',264,'20');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('An Uncle''s Letters','12','Fiction','2012-06-12',258,'20');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('Pretty flowers','13','Album','2013-01-31',148,'22');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('A Tale of Lions','12','Fantasy','2012-08-17',301,'21');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('eRobot','13','Sci Fi','2012-10-04',465,'23');

标签: sqlsql-serverssmsdate-formatting

解决方案


执行insert. _ 列名和值的顺序必须相同:

insert into book (BookName, WriterID, Genre, DateOfPublication, , NoOfPages, EditorID)
    values ('Valley of Heroes', '10', 'Fiction', '2010-01-12', 874, '20');

推荐阅读