简体   繁体   English

从字符串错误转换日期和/或时间时,MSSQL Server 2008转换失败

[英]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.

相关问题 转换输入的时间时从字符串转换日期和/或时间时转换失败| SQL,SQLSRV - Conversion failed when converting date and/or time from character string when converting inputted time | SQL, SQLSRV 无法更正我的代码:从字符串转换日期和/或时间时转换失败 - Failed to correct my codes: Conversion failed when converting date and/or time from character string 如何修复“未捕获的 PDOException:...从字符串转换日期和/或时间时转换失败”? - How to fix "Uncaught PDOException: ...Conversion failed when converting date and/or time from character string"? 从字符串转换日期和/或时间时转换失败 - 但不明白为什么 - Conversion failed when converting date and/or time from character string - but no idea why 从字符串转换日期和/或时间时,PHP mssql_query错误 - PHP mssql_query errors when converting date and/or time from character string 使用 PHP 和 PDO 与 MS SQL 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string using PHP and PDO with MS SQL 使用Java日期选择器时从字符串转换日期时间的语法错误 - Syntax error converting datetime from character string when using Java Date Picker 转换varchar值错误时转换失败 - Conversion failed when converting the varchar value error 用Php和SQL Server将日期时间转换为字符串时出现问题 - Problem when converting a date time to string with Php and SQL Server PHP输入MS SQL无法从字符串转换日期和/或时间 - PHP Input MS SQL Failed to convert date and/or time from character string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM