INSERT INTO Tasks(TaskCreateDate, TaskDescription, TaskFinnishDate, TaskArchiveDate, GroupID, EmployeeID)
VALUES ('20/01/2019', 'abc', '21/01/2019', '21/01/2019', (SELECT GroupID from TypeOfGroup WHERE GroupID='1'), (SELECT EmployeeID from Employees WHERE EmployeeID='1')),
('21/02/2019', 'abc', '22/02/2019', '22/02/2019', (SELECT GroupID from TypeOfGroup WHERE GroupID='2'), (SELECT EmployeeID from Employees WHERE EmployeeID='1')),
('22/03/2019', 'abc', '23/03/2019', '23/03/2019', (SELECT GroupID from TypeOfGroup WHERE GroupID='3'), (SELECT EmployeeID from Employees WHERE EmployeeID='1'));
我收到错误:
Msg 242,Level 16,State 3,Line 1
将varchar数据类型转换为日期时间数据类型会导致超出范围的值。
答案 0 :(得分:5)
在几乎所有数据库(包括SQL Server)中,您只需使用ISO / ANSI标准格式:
INSERT INTO Tasks(TaskCreateDate, TaskDescription, TaskFinnishDate, TaskArchiveDate, GroupID, EmployeeID)
VALUES ('2019-01-20', 'abc', '2019-01-21', '2019-01-21',
(SELECT GroupID from TypeOfGroup WHERE GroupID = 1),
(SELECT EmployeeID from Employees WHERE EmployeeID = 1)
),
. . .
在此类常量之前,某些数据库支持(并且Oracle要求)ANSI标准关键字date
。
请注意,我删除了" 1" s周围的单引号以进行ID比较。这假设该值是数字。仅对字符串和日期常量使用单引号。
请注意,在SQL Server中,2017-01-21 几乎总是表示YYYY-MM-DD。有一个国际环境,它被解释为YYYY-DD-MM。如果您想要真正的弹性,请删除连字符,'20170121'
始终在SQL Server中解释为YYYYMMDD。