4

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
) ON [PRIMARY]

I'm getting data by a code:

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;";
    cin.get();
    SQLDisconnect(connectHandle);

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.

amaranth
  • 979
  • 2
  • 22
  • 40
  • 2
    Maybe those links can help you : https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000013705031 and http://docs.raima.com/rdms/8_3/Content/RM/Chapter17.htm – Garf365 Mar 26 '15 at 08:41

1 Answers1

6

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

   TIMESTAMP_STRUCT ts;
   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.day, ts.hour, ts.minute, ts.second, ts.fraction, len);

Hope this help.

Louis
  • 660
  • 1
  • 7
  • 14