duanaoyuan7202 2013-08-03 00:05
浏览 64
已采纳

使用Java Date Picker时,语法错误从字符串转换datetime

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.

  • 写回答

1条回答 默认 最新

  • dougan1465 2013-08-03 16:48
    关注

    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 Y-m-d format instead of Y-m-d 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作