Dear all,
How to use DATE_FORMAT on query with zdb?
I have a query which use DATE_FORMAT( DATE_START, '%d-%m-%Y %k:%i:%s' ) AS DATE_START The return from ResultSet_getStringByName( r, "DATE_START" ) is -1208391138-Success-2012 16:0:(null)
The data on table is 2012-04-19 16:04:00 so the result should be 19-04-2012 16:04:00
Please help me, Thanks
This is more a C question than a libzdb question, but anyway, you cannot write '%d-%m-%Y %k:%i:%s' directly in a query string since Connection_executeQuery take a variable argument list and will parse the above date-format as such a statement and expect variables to be provided.
You have two options, either use a prepared statement and set the date format as a string or provide the date-format in its own string format (%s) like so:
1) Prepared statement:
PreparedStatement_T p = Connection_prepareStatement(c, "select <..> ? <..>"); PreparedStatement_setString(p, 1, "DATE_FORMAT( DATE_START, '%d-%m-%Y %k:%i:%s' ) AS DATE_START"); ResultSet_T r = PreparedStatement_executeQuery(p); while (ResultSet_next(r)) ...
2) Direct:
ResultSet_T r = Connection_executeQuery(c, "select <..> %s <..>;", "DATE_FORMAT( DATE_START, '%d-%m-%Y %k:%i:%s' ) AS DATE_START", …) while (ResultSet_next(r)) ...
On Jul 25, 2012, at 6:30 AM, Hans Richardo wrote:
Dear all,
How to use DATE_FORMAT on query with zdb?
I have a query which use DATE_FORMAT( DATE_START, '%d-%m-%Y %k:%i:%s' ) AS DATE_START The return from ResultSet_getStringByName( r, "DATE_START" ) is -1208391138-Success-2012 16:0:(null)
The data on table is 2012-04-19 16:04:00 so the result should be 19-04-2012 16:04:00
Please help me, Thanks
On Jul 25, 2012, at 3:53 PM, Jan-Henrik Haukeland wrote:
You have two options, either use a prepared statement and set the date format as a string or provide the date-format in its own string format (%s) like so:
Sorry for the extra spam, but I just remembered that there is a third option in where you "escape" % by using %%.
ResultSet_T r = Connection_executeQuery(c, "select DATE_FORMAT( DATE_START, '%%d-%%m-%%Y %%k:%%i:%%s' ) AS DATE_START..") while (ResultSet_next(r))