简体   繁体   中英

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. 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.

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. The Date/Time are obtained from a Java date picker script. Everything on the two pages are exact, except for the sql query. Does anyone have any idea what could be the problem? I am fairly new to PHP and so I am learning as I go.

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. 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). 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.

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. Add the DateAdded IS NOT NULL in where clause.

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? 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM