简体   繁体   中英

Problematic DateTime Comparison SQL Query Not Working

I have a problem with executing ODBC query. I am trying to achieve something simple.

SELECT t.* FROM table t WHERE DateTime > '#2014-05-05 00:00:00#'

I have tried to execute with or without hashes (#) and quotes around the datetime, but no success with that. Also, instead of using >, < and other comparison operators I have tried to use BETWEEN .

In addition several formats "dd/mm/yyyy", "mm/dd/yyyy", "dd.mm.yyyy", etc. plus standard sql format (iso) were tested and the result is the same.

The approach looks like:

$sql = "SELECT t.* FROM table t WHERE DateTime > '#...#'";
$result = odbc_fetch_array(odbc_exec($odbc_connection, $sql));

After this execution I receive some 5-10 symbol binary error, but no error message (with odb_error and odbc_errormsg), in addition to the:

No tuples available at this result index

(The topic: ODBC error in PHP: “No tuples available at this result index” is not helping)

Could it be some ODBC issue (old version etc.) or we should cast another black magic through odbc_exec ?

Maybe ODBC have trouble with reserved word datetime. I don't know how to escape quotes in PHP, try

SELECT t.* FROM table t WHERE t."DateTime" > '20140505'";

You should use ODBC syntax for datetimes which is { ts '1998-05-02 01:23:56.123' } . You can omit the partial seconds. See here .

Have you tried using the TO_DATE() function? You can specify the format you are using.

SELECT t.* FROM table t WHERE DateTime > TO_DATE('2014-05-05 00:00:00','YYYY-MM-DD HH24:MI:SS')

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