Queries involving where clause with timestamp data type
719397Aug 26 2009 — edited Aug 26 2009I need to use milliseconds in my tables and do queries where I compare the time in the table to a specific time. I changed the column to the TIMESTAMP data type (from DATE) to accommodate the milliseconds.
I am accessing the data via a C++ program using ODBC to access the Oracle 10g XE database and a SQLBindParameter statement to set up the comparisons.
Below is a table that looks like mine but simplified to just a few fields:
. DATA_ID NOT NULL NUMBER(10)
. START_TIME NOT NULL TIMESTAMP(6)
. STOP_TIME NOT NULL TIMESTAMP(6)
My query is:
. string sql("select "\
. "to_Char(rv.start_time, 'YYYYMMDD HH24:MI:SS.FF3'), "\
. "to_Char(rv.stop_time, 'YYYYMMDD HH24:MI:SS.FF3'), "\
. "rv.data_id "\
. "from "\
. "Reservation rv "\
. "where "\
. "rv.data_id = ? and "\
. "rv.stop_time >= to_timestamp(?, 'YYYYMMDD HH24:MI:SS.FF3') and "\
. "rv.start_time <= to_timestamp(?, 'YYYYMMDD HH24:MI:SS.FF3');");
MY C++ code (minus the usual statement preparation material) is:
. string startTime("20090826 15:42:02.000");
. string stopTime("20110828 02:04:52.000");
.
. SQLRETURN rc = SQL_SUCCESS;
.
. SQLINTEGER sqlRsrcInstanceId = resource->getResourceInstanceId();
. SQLLEN sqlRsrcInstanceIdLI = 0;
. rc |= SQLBindParameter( stmtHandle, 1, SQL_PARAM_INPUT,
. SQL_C_LONG, SQL_INTEGER, 0, 0,
. &sqlRsrcInstanceId, 0, &sqlRsrcInstanceIdLI );
.
. SQLCHAR sqlStartTime[24];
. memset(sqlStartTime, 0, 24);
. SQLLEN sqlStartTimeLI = SQL_NTS;
.
. strncpy( (char*)sqlStartTime, startTime, 23);
. rc |= SQLBindParameter( stmtHandle, 2, SQL_PARAM_INPUT,
. SQL_C_CHAR, SQL_CHAR,
. sizeof(sqlStartTime)-1, 0,
. &sqlStartTime, sizeof(sqlStartTime)-1,
. &sqlStartTimeLI);
.
. SQLCHAR sqlStopTime[24];
. memset(sqlStopTime, 0, 24);
. SQLLEN sqlStopTimeLI = SQL_NTS;
.
. strncpy( (char*)sqlStopTime, stopTime, 23);
. rc |= SQLBindParameter( stmtHandle, 3, SQL_PARAM_INPUT,
. SQL_C_CHAR, SQL_CHAR,
. sizeof(sqlStopTime)-1, 0,
. &sqlStopTime, sizeof(sqlStopTime)-1,
. &sqlStopTimeLI);
When the statement is executed, I get the following error:
. ODBC Return Code: SQL Error.
. Native Error Code/Message:
. 932/[Oracle][ODBC][Ora]ORA-00932: inconsistent datatypes: expected - got -
Any suggestions on what is wrong or a better way to perform the query?