[英]Conversion failed when converting date and/or time from character string
I have this SQL Server query which is returning Conversion failed when converting date and/or time from character string.
我有此SQL Server查询,
Conversion failed when converting date and/or time from character string.
,返回Conversion failed when converting date and/or time from character string.
(
@numberRecords int,
@date varchar(12)
)
AS
SET ROWCOUNT @numberRecords
SELECT re.ID, re.RecentChangeTypeID, re.LogID, re.RevisionID
FROM RecentChanges re
FULL OUTER JOIN Revisions rev
ON re.RevisionID = rev.ID
FULL OUTER JOIN Logs lg
ON re.LogID = lg.ID
WHERE convert(datetime, rev.Timestamp) >= '@date' OR
convert(datetime, lg.Timestamp) >= '@date';
RETURN
The date
parameter returns a string value in the format "dd-MM-yyyy". date
参数返回格式为“ dd-MM-yyyy”的字符串值。 What may be the cause? 可能是什么原因?
Don't put your @date
parameter inside single quotes - it's being interpreted as a string instead of as a parameter. 不要将
@date
参数放在单引号内-它被解释为字符串而不是参数。
As written you are trying to make '@date'
a datetime
which won't parse. 如所写,您试图将
'@date'
为无法解析的datetime
。
That being said, you should probably make the input parameter a datetime
in the first place. 话虽如此,您可能应该首先将输入参数设置为
datetime
时间。
remove the single quotes as @JNK has described to make your query work. 删除@JNK描述的单引号以使您的查询正常工作。 However, for possibly much better performance (index usage) try this query (if you have indexes on the columns
Revisions.Timestamp
and Logs.Timestamp
) : 但是,为了获得更好的性能(索引使用率),请尝试以下查询(如果您在
Revisions.Timestamp
和Logs.Timestamp
列上具有索引):
SELECT
re.ID, re.RecentChangeTypeID, re.LogID, re.RevisionID
FROM RecentChanges re
INNER JOIN Revisions rev ON re.RevisionID = rev.ID
WHERE rev.Timestamp >= @date
UNION
SELECT
re.ID, re.RecentChangeTypeID, re.LogID, re.RevisionID
FROM RecentChanges re
INNER JOIN Logs lg ON re.LogID = lg.ID
WHERE lg.Timestamp >= @date
it looks to me, at first glance, that you are converting to datetime, not from it. 乍一看,我觉得您正在转换为日期时间,而不是日期时间。 I am assuming rev.Timestamp is a datetime already, while your @date is a varchar(12)...so SQL Server is complaining that it can't compare a datetime to a string...
我假设rev.Timestamp已经是一个日期时间,而您的@date是一个varchar(12)...所以SQL Server抱怨说它无法将日期时间与字符串进行比较...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.