声明日期转换错误

时间:2014-05-13 16:43:30

标签: sql datetime declare

不确定这里有什么问题,但有人可以帮助我。我不想在SQL中声明日期时间。

declare @START datetime2 = '2013-07-17 00:00:00.00';
declare @END datetime2 = '2013-07-17 23:59:99.99';

select a.scheduledstart as StartTime, a.scheduledend as EndTime, a.ScheduledDurationMinutes as ScheduledDuration, a.activitytypecode, a.statecode, a.statuscode, a.OwnerId, b.new_SalesrepId as SalesRepID from ActivityPointerBase as a
join SystemUserExtensionBase as b on b.SystemUserId = a.OwnerId
where a.ActivityTypeCode = '4201' and a.StateCode = '1' and a.StatusCode = '3' and b.new_SalesrepId = 'CJV00100' and (a.ScheduledStart >= @START AND a.ScheduledEnd <= @END)

抛出此错误: Msg 241,Level 16,State 1,Line 2 从字符串转换日期和/或时间时转换失败。

2 个答案:

答案 0 :(得分:1)

您正在尝试为@END变量声明无效时间。您在秒数部分中不能有99,因此请将其更改为:

declare @START datetime2 = '20130717 00:00:00.00';
declare @END datetime2 = '20130717 23:59:59.99';

我还删除了日期中的-,以避免使用特定语言格式。

答案 1 :(得分:0)

你不能这样做:

declare @END datetime2 = '2013-07-17 23:59:99.99';

这样做:

declare @END datetime2 = '2013-07-17 23:59:59.99';
                                           ^^

记住它是年 - 月 - 日时:分:秒。分数和分钟从0到59,而不是99。

此外,我更喜欢使用这样的结束日期:

declare @END datetime2 = '2013-08-17'; --use "less than" the next day and forget time
WHERE yourColumn <@END