How to get SQL Server DateTime field in ODBC native client

I’m have SQL Server table:

CREATE TABLE [dbo].[Table1](
    [rec_id] [int] IDENTITY(1,1) NOT NULL,
    [id] [int] NOT NULL,
    [date] [datetime] NOT NULL,
    [ps] [varchar](200) NULL

I’m getting data by a code:

  • What's the best way to identify hidden characters in the result of a query in SQL Server (Query Analyzer)?
  • SQL Server not working after upgrading windows8.1 to windows 10
  • SQL Server: Splitting string by '/' and put each split element into a different column
  • SQL Server: Count the number of times the ID from table A occurs in table B
  • How do I change my membership database from express to standard sql?
  • SQL Server database restore error: specified cast is not valid. (SqlManagerUI)
  • status = SQLExecDirect(statement, (SQLWCHAR*)TEXT("SELECT * FROM [DBNAME].[dbo].[Table1]"), SQL_NTS);
    cout << "SQLExecDirect returned " << status << "\r\n";
        if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
            int rec_id;
            int id;
            char date[64];
            char ps[201] = { 0 };
            while (SQLFetch(statement) == SQL_SUCCESS)
                SQLGetData(statement, 1, SQL_C_ULONG, &rec_id, 0, NULL);
                SQLGetData(statement, 2, SQL_C_ULONG, &id, 0, NULL);
                SQLGetData(statement, 3, SQL_C_CHAR, date, 64, NULL);
                SQLGetData(statement, 4, SQL_C_CHAR, ps, 201, &len);
                cout << rec_id << " " << id << " " << date << " " << ps << " " << len << endl;
        cout << "Connected;";

    But I’m get date field as char array in output: 2014-01-01 00:00:00.000
    How to get this field, for example, in FILETIME format or in other more convenient C++ data type? Thanks.

  • T-SQL: Lock a table manually for some minutes
  • SQL Server 2008 compatibility with SQL Server 2005
  • Overflow exception when reading decimal values from SQL Server
  • Error with SqlBulkCopyColumnMapping
  • Using correlated subquery
  • how to separate string into different columns?
  • One Solution collect form web for “How to get SQL Server DateTime field in ODBC native client”

    For date type, you should use SQL_C_DATE or SQL_C_TIMESTAMP. Example:

       SQLLEN           len;
       SQLGetData(hstmt, 3, SQL_C_TIMESTAMP, &ts, sizeof(ts), &len);
       printf("Date: %d-%d-%d %d:%d:%d.%d (%ld)\n",
               ts.year, ts.month,, ts.hour, ts.minute, ts.second, ts.fraction, len);

    Hope this help.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.