[英]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. 您可以通过检查DateAdded
和DateUpdated
是否不为空来避免上述错误。 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. 如果使用正确的日期填充DateAdded
和DateUpdated
字段( 请在此处检查 ),则可以避免每次从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.