Working with date/time includes the following:
- Binding date/time values into input variables (SQL statements) or input parameters (stored procedures/functions)
- Reading date/time values (from fields in result set or from output parameters of stored procedure)
Binding date/time values
Let's say we want to update date/time field named FDATETIME
from table named TEST
with the value of October 6, 2000 19:00:00
.
First, let's create a sample command that requires a datetime binding:
SACommand cmd(
&con,
_TSA("Update TEST set FDATETIME = :1"));
You need an instance of SADateTime class to do the bind. There are several ways to create it. First is to construct it explicitly:
SADateTime dtValue(
2000 /*year*/, 10 /*October*/, 6 /*day of month*/,
19 /*hours*/, 0 /*minutes*/, 0 /*seconds*/);
Alternatively, you can convert to SADateTime from an existing C struct tm:
extern struct tm some_tm_Value;
SADateTime dtValue = some_tm_Value;
Or you can also convert from an instance of Windows DATE data type:
extern DATE some_DATE_Value;
SADateTime dtValue = some_DATE_Value;
Finally, bind the date/time value and execute:
cmd << dtValue;
cmd.Execute();
Reading date/time values
Let's say we want to retrieve date/time field named FDATETIME
from table named TEST
.
Let's create and execute a sample SQL command that returns data of datetime type (for more information see Execute SQL Command):
SACommand cmd(
&con,
_TSA("SELECT FDATETIME FROM TEST"));
cmd.Execute();
Now we can fetch the data and access date/time column values using SADateTime, standard C struct tm or Windows DATE data type:
while(cmd.FetchNext())
{
SADateTime dtValue = cmd.Field(_TSA("FDATETIME"));
// access through standard C struct tm
struct tm tmValue = dtValue;
struct tm tmValue2 = (struct tm)cmd.Field(_TSA("FDATETIME")).asDateTime();
...
// access through Windows DATE data type
DATE dateValue = dtValue;
DATE dateValue2 = (DATE)cmd.Field(_TSA("FDATETIME")).asDateTime();
...
}