DATE and DATETIME data types

Informix®

Informix provides two data types to store date and time information:

  • DATE = for year, month and day storage.
  • DATETIME = for year to fraction (1-5) storage.

The DATE type is stored as an INTEGER with the number of days since 1899/12/31.

The DATETIME type can be defined with various time units, by specifying a start and end qualifier. For example, you can define a datetime to store an hour-to-second time value with DATETIME HOUR TO SECOND.

The values of Informix DATETIME can be represented with a character string literal, or as DATETIME() literals:
'2017-12-24 15:45:12.345'  -- a DATETIME YEAR TO FRACTION(3)
'15:45'   -- a DATETIME HOUR TO MINUTE
DATETIME(2017-12-24 12:45) YEAR TO MINUTE
DATETIME(12:45:56.333) HOUR TO FRACTION(3)
Informix is able to convert quoted strings to DATE / DATETIME data, if the string contains matching environment parameters. The string to date conversion rules for DATE is defined by the DBDATE environment variable. The string to datetime format for DATETIME is defined by the GL_DATETIME environment variable.
Note: Within Genero programs, the string representation for DATETIME values is always ISO (YYYY-MM-DD hh:mm:ss.fffff)

Informix supports date arithmetic on DATE and DATETIME values. The result of an arithmetic expression involving dates/times is an INTEGER number of days when only DATE values are used, and an INTERVAL value if a DATETIME is used in the expression.

Informix automatically converts an INTEGER to a DATE when the integer is used to set a value of a date column.

Microsoft™ SQL Server

Microsoft SQL Server (2008+) provides the following data type to store date and time data:

Table 1. Microsoft SQL Server date/time data types
Microsoft SQL Server data type Description
DATE for year, month, day storage
DATETIME for year, month, day, hour, min, second, fraction(3) storage (from January 1, 1753 through December 31, 9999). Values are rounded to increments of .000, .003, or .007 seconds
SMALLDATETIME for year, month, day, hour, minutes storage (from January 1, 1900, through June 6, 2079). Values with 29.998 seconds or lower are rounded down to the nearest minute; values with 29.999 seconds or higher are rounded up to the nearest minute
TIME(n) for hour, minute, second and fraction(7) storage. Where n defines the precision of fractional seconds
DATETIME2(n) for year, month, day, hour, minute, second and fraction(7) storage. Where n defines the precision of fractional seconds
DATETIMEOFFSET(n) for year, month, day, hour, minute, second, fraction(7) and time zone information storage. Where n defines the precision of fractional seconds

Like Informix, Microsoft SQL Server can convert quoted strings to DATETIME data. The CONVERT() SQL function allows you to convert strings to dates.

Microsoft SQL Server does not allow direct arithmetic operations on datetimes; the date handling SQL functions must be used instead (DATEADD and DATEDIFF).

SQL Server provides equivalent functions for Informix YEAR(), MONTH() and DAY(). Take care with the DAY() function on SQL Server because it begins from January 1, 1900 while Informix begins from December 31, 1899.

Table 2. Select first day example (Informix vs. Microsoft SQL Server)
Informix Microsoft SQL SERVER
SELECT day(0), month(0), year(0)
  FROM systables WHERE tabid=1;
------ ------ ------
    31     12   1899
SELECT day(0), month(0), year(0)
----------- ----------- -----------
          1           1        1900

The SQL Server equivalent for Informix WEEKDAY() is the DATEPART(dw,date-value) function.

The weekday date part depends on the value set by SET DATEFIRST n, which sets the first day of the week (1=Monday ... 7=Sunday (default)).

Solution

Use the following conversion rules to map Informix date/time types to Microsoft SQL Server date/time types:

Table 3. Informix data types and Microsoft SQL Server equivalents
Informix data type Microsoft SQL Server data type
DATE DATE
DATETIME HOUR TO MINUTE TIME(0)
DATETIME HOUR TO SECOND TIME(0)
DATETIME HOUR TO FRACTION(n) TIME(n)
DATETIME YEAR TO MONTH DATETIME2(0)
DATETIME YEAR TO DAY DATETIME2(0)
DATETIME YEAR TO HOUR DATETIME2(0)
DATETIME YEAR TO MINUTE DATETIME2(0)
DATETIME YEAR TO SECOND DATETIME2(0)
DATETIME YEAR TO FRACTION(n) DATETIME2(n)
The DATE and DATETIME types translation can be controlled with the following FGLPROFILE entries:
dbi.database.dsname.ifxemul.datatype.date = { true | false }
dbi.database.dsname.ifxemul.datatype.datetime = { true | false }
For more details see IBM Informix emulation parameters in FGLPROFILE.

SQL Server and Informix DATE data type are equivalent and store year, month, day values.

SQL Server TIME(n) data type can be used to store Informix DATETIME HOUR TO FRACTION(n), DATETIME HOUR TO SECOND, DATETIME HOUR TO MINUTE values, and any other DATETIME type with qualifiers HOUR, MINUTE, SECOND and FRACTION(n). Missing time parts default to 00:00:00.0. For example, when using a DATETIME MINUTE TO FRACTION(3) with the value of "45:23.999", the SQL Server TIME(3) value will be "00:45:23.999".

Informix DATETIME values with any qualifiers from YEAR to FRACTION(5) can be stored in SQL Server DATETIME2(n) columns. Missing date or time parts default to 1900-01-01 00:00:00.0. For example, when using a DATETIME DAY TO MINUTE with the value of "23 11:45", the SQL Server DATETIME(0) value will be "1900-01-23 11:45:00".

Important:
  • When fetching a TIME or DATETIME2 with a precision that is greater than 5 (the DATETIME precision limit), the database interface will allocate a buffer of VARCHAR(16) for the TIME and VARCHAR(27) for the DATETIME2 column. As a result, you can fetch such data into a CHAR or VARCHAR variable.
  • Review the program logic if you are using the Informix WEEKDAY() function because SQL Server uses a different basis for the days numbers ( Monday = 1 ).
  • Use the SQL Server's GETDATE() function to get the system current date.