简体   繁体   English

使用Java日期选择器时从字符串转换日期时间的语法错误

[英]Syntax error converting datetime from character string when using Java Date Picker

I am trying to do a query into a database to return a report with php. 我正在尝试对数据库进行查询以使用php返回报告。 I am using duplicate pages as they both are pulling from the same database/tables. 我正在使用重复的页面,因为它们都是从同一数据库/表中提取的。

The specific error I receive is: 我收到的具体错误是:

Msg 241, Level 16, State 1, Line 1 Syntax error converting datetime from character string. 消息241,级别16,状态1,行1语法错误从字符串转换datetime。

This query works and returns the desired data: 该查询有效并返回所需的数据:

REMOVED SELECT STATEMENT PER COMPANY POLICY 根据公司政策修订的选择声明

This query however, does not work: 但是,此查询不起作用:

REMOVED SELECT STATEMENT PER COMPANY POLICY 根据公司政策修订的选择声明

I changed the DateUpdated to DateAdded in the cast portion. 我在演员表部分中将DateUpdated更改为DateAdded。 The Date/Time are obtained from a Java date picker script. 日期/时间是从Java日期选择器脚本获得的。 Everything on the two pages are exact, except for the sql query. 除sql查询外,两页上的所有内容都是准确的。 Does anyone have any idea what could be the problem? 有谁知道可能是什么问题? I am fairly new to PHP and so I am learning as I go. 我对PHP相当陌生,因此我正在学习。

I have tried searching but I do not understand what I should do. 我曾尝试搜索,但不知道该怎么办。

I have these declared from the picker.... 我已经从选择器中声明了这些。

$startunix     = date("Y-m-d H:i:s", strtotime($startdate));
$startunixmn   = date("Y-m-d 23:59:59", strtotime($startdate));
$endunix       = date("Y-m-d H:i:s", strtotime($enddate));
$endunixmn     = date("Y-m-d 23:59:59", strtotime($enddate));

and within the sql query, in place of the dates/times that are showing in the statement, they have $startunix and $endunixm for the two dates/times. 并且在sql查询中,代替语句中显示的日期/时间,它们在两个日期/时间中具有$ startunix和$ endunixm。 Are you not allowed to change what is in the cast? 您是否不允许更改演员表中的内容? I thought you would be able to, because both columns are dates in the format 010109 (mmddyy). 我认为您可以,因为两列的日期均为010109(mmddyy)格式。 SO I figured if you change the DateUpdated to DateAdded (titles of the two columns) then the query should work, just based off of the date range selected for DateAdded instead of DateUpdated. 所以我想如果您将DateUpdated更改为DateAdded(两列的标题),则查询应该工作,只是基于为DateAdded而不是DateUpdated选择的日期范围。

Any help or insight would be hugely appreciated. 任何帮助或见解将不胜感激。 If you need more info, please ask and I will do my best to provide what I can. 如果您需要更多信息,请询问,我将尽我所能。

In your case you will avoid the mentioned error by checking the if the DateAdded and DateUpdated are not null. 您可以通过检查DateAddedDateUpdated是否不为空来避免上述错误。 Add the DateAdded IS NOT NULL in where clause. 在where子句中添加DateAdded IS NOT NULL

It would be the best to insert once prepared value in database instead of parsing it every time you query the db. 最好在数据库中插入一次准备好的值,而不是每次查询数据库时都对其进行解析。 You said you are using: 您说您正在使用:

$startunix     = date("Y-m-d H:i:s", strtotime($startdate));
$startunixmn   = date("Y-m-d 23:59:59", strtotime($startdate));
$endunix       = date("Y-m-d H:i:s", strtotime($enddate));
$endunixmn     = date("Y-m-d 23:59:59", strtotime($enddate));

but where does the string in mmddYY format then comes from? 但是mmddYY格式的字符串从何而来? If you populate the DateAdded and DateUpdated fields with proper date ( check it here ) you will be able to avoid making a new value from DateAdded field every time. 如果使用正确的日期填充DateAddedDateUpdated字段( 请在此处检查 ),则可以避免每次从DateAdded字段中输入新值。 Also if you don't need time, you can use Ymd format instead of Ymd H:i:s and will be able to shorten your query to something like this: 另外,如果您不需要时间,可以使用Ymd格式而不是Ymd H:i:s并且可以将查询缩短为以下形式:

SELECT CardNo as CardNo, LastName as CLname, FirstName as CFname, Email as CEmail ,DateUpdated as CUpdated, DateAdded as CEntered FROM cards
WHERE recemail = 'YES' AND email IS NOT NULL AND email <> '' AND (DateAdded BETWEEN '2013-07-29' AND '2013-08-02') ORDER BY DateAdded desc, CardNo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 PHP 和 PDO 与 MS SQL 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string using PHP and PDO with MS SQL 从字符串错误转换日期和/或时间时,MSSQL Server 2008转换失败 - MSSQL Server 2008 Conversion failed when converting date and/or time from character string Error 将字符串转换为日期和日期时间 - Converting string to Date and DateTime 转换输入的时间时从字符串转换日期和/或时间时转换失败| SQL,SQLSRV - Conversion failed when converting date and/or time from character string when converting inputted time | SQL, SQLSRV 将日期转换为日期时间时出错 - Error converting date to datetime 使用php将非标准日期字符串转换为mysql datetime字符串 - Converting a non standard date string to a mysql datetime string using php 将字符串日期和时间转换为datetime - Converting string date and time to datetime 如何修复“未捕获的 PDOException:...从字符串转换日期和/或时间时转换失败”? - How to fix "Uncaught PDOException: ...Conversion failed when converting date and/or time from character string"? 从字符串转换日期和/或时间时,PHP mssql_query错误 - PHP mssql_query errors when converting date and/or time from character string 无法更正我的代码:从字符串转换日期和/或时间时转换失败 - Failed to correct my codes: Conversion failed when converting date and/or time from character string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM