[英]MSSQL Server 2008 Conversion failed when converting date and/or time from character string Error
I am currently receiving the MSSQL Server 2008 error: Conversion failed when converting date and/or time from character string.
我当前收到MSSQL Server 2008错误: Conversion failed when converting date and/or time from character string.
I've checked the following threads but am still having no lucks: 我检查了以下线程,但仍然没有任何不足:
Conversion failed when converting date and/or time from character string while inserting datetime Conversion failed when converting date and/or time from character string in SQL SERVER 2008 在插入datetime时从字符串 转换日期和/或时间时转换失败在SQL SERVER 2008中从字符串转换日期和/或时间时转换失败
Here is the query (I'm using PHP's PDO SQLSRV): 这是查询(我正在使用PHP的PDO SQLSRV):
SELECT ISNULL(MYDATE,'None') AS MYDATE,
ISNULL(CAST(rnm AS NVARCHAR(50)),'None') AS Rnm,
DATENAME(dw, MYDATE) as nameOfDay,
FROM tab1
INNER JOIN rnm ON (tab1.rte = rnm.rte)
WHERE DATEDIFF(S, '1970-01-01', MYDATE) >= :startDate
AND DATEDIFF(S, '1970-01-01', MYDATE) <= :endDate
GROUP BY MYDATE, CAST(rnm AS NVARCHAR(50)) WITH ROLLUP
The MYDATE field is of the type datetime
in MSSQL. 在MSSQL中,MYDATE字段的类型为datetime
。 I have tried casting each of the MYDATE variables and the 1970-01-01
all as datetime, but I still receive the error. 我尝试将每个MYDATE变量和1970-01-01
都强制转换为日期时间,但仍然收到错误。
The "rnm" is cast as VARCHAR because it's an old DB using the "text" type still, unfortunately. 不幸的是,“ rnm”被强制转换为VARCHAR,因为它仍然是使用“文本”类型的旧数据库。
Thanks. 谢谢。
---update: ---更新:
$startDate = strtotime($_GET['startDate'] . " 00:00:00");
$endDate = strtotime($_GET['endDate'] . " 23:59:59");
The above is then bound to the prepared variable via bindParam()
. 然后,通过bindParam()
将以上内容绑定到准备好的变量。
You are grouping by MYDATE which is datetime instead of IsNull(MYDATE, 'None') which is varchar and sometimes stores 'None' as a value. 您正在按日期时间MYDATE分组,而不是由varchar的IsNull(MYDATE,'None')分组,有时将'None'作为值存储。 All those DATEDIFFs and conversions inside WHERE clause will lead to performance issues. WHERE子句中的所有那些DATEDIFF和转换将导致性能问题。 Try to remove DATEDIFFs and pass startdate/enddate in datetime format. 尝试删除DATEDIFF,并以datetime格式传递startdate / enddate。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.