Informix - Combining date and time field

Luke Sharma

We receive data in this format from informix tables

Run Date        Run Time
2017-05-22        0
2017-05-22        100
2017-05-22        200
2017-05-22        300
2017-05-22        400

Expected Output:

2017-05-22 04:00

I want combine both to get the datetime, so that manipulations can be done. Please help.

Jonathan Leffler

The question doesn't identify the types of the columns for the run date and run time values. However, the plausible types for run date are DATE, DATETIME YEAR TO DAY, and CHAR(10); the plausible types for run time are INTEGER and CHAR(4). For this exercise, there's no difference between INTEGER and SMALLINT, or CHAR(n) and VARCHAR(n), and you could (but shouldn't) use a DECIMAL for the run time. A properly specified question would avoid these problems.

Fortunately, it doesn't matter much — the same basic SQL code handles all the variations above — though you might need to add more casts for some of the more outré type combinations.

This code assumes you have DBDATE="Y4MD-" or equivalent set in the environment; you have to make changes if you both use a DATE column and a different value for $DBDATE.

DATE plus INTEGER

DROP TABLE IF EXISTS rd_rt_date_integer;
CREATE TABLE rd_rt_date_integer
(
    run_date    DATE NOT NULL,
    run_time    INTEGER NOT NULL
                CHECK (run_time >= 0 AND run_time < 2400 AND MOD(run_time, 100) < 60),
    PRIMARY KEY(run_date, run_time)
);

INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 0);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 100);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 200);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 300);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 400);

SELECT run_date, run_time,
       EXTEND(run_date, YEAR TO MINUTE) +
       MOD(run_time, 100) UNITS MINUTE +
       (run_time / 100) UNITS HOUR AS run_date_time
  FROM rd_rt_date_integer;

Example output

run_date    run_time    run_date_time
DATE        INTEGER     DATETIME YEAR TO MINUTE
2017-05-22  0           2017-05-22 00:00
2017-05-22  100         2017-05-22 01:00
2017-05-22  200         2017-05-22 02:00
2017-05-22  300         2017-05-22 03:00
2017-05-22  400         2017-05-22 04:00

CHAR plus CHAR

DROP TABLE IF EXISTS rd_rt_char_char;
CREATE TABLE rd_rt_char_char
(
    run_date    CHAR(10) NOT NULL
                CHECK (run_date MATCHES '[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]'),
    run_time    CHAR(4) NOT NULL,
    PRIMARY KEY(run_date, run_time)
);

INSERT INTO rd_rt_char_char VALUES('2017-05-22', '0');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '100');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '200');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '300');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '400');

SELECT run_date, run_time,
       EXTEND(run_date::DATE, YEAR TO MINUTE) +
       MOD(run_time, 100) UNITS MINUTE +
       (run_time / 100) UNITS HOUR AS run_date_time
  FROM rd_rt_char_char;

Example output

run_date    run_time    run_date_time
CHAR(10)    CHAR(4)     DATETIME YEAR TO MINUTE
2017-05-22  0           2017-05-22 00:00
2017-05-22  100         2017-05-22 01:00
2017-05-22  200         2017-05-22 02:00
2017-05-22  300         2017-05-22 03:00
2017-05-22  400         2017-05-22 04:00

For the most part, Informix is very good about automatically converting between types — the need to cast to DATE here (it could be to DATETIME YEAR TO DAY instead) is necessary because the optimizer doesn't know which of the myriad DATETIME sub-types to use when considering run_date as a DATETIME. (OK: so 'myriad' is an exaggeration. There are 56 distinct DATETIME sub-types and 7 synonyms for some of those types.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combining time & date in golang?

C combining date and time value

Combining Date and Time input strings as a Date object

Python date time field

Postgresql combining date, time and microseconds into timestamp

Combining date and time (timestamp) in a list of dataframes

Combining a parsed time with today's date in Python

Android: Combining the values of Date and Time into one value

Combining date and time in clojure to return an instant

Informix - Need to create date time parameters for Where clause

Convert Integer field Date time to Date Time

Combining two Date instance to create date-time using Joda

Create a new Data time Column by combining separate Date and Time Column

Date field keeps showing time

Access Date/Time field updates

Elasticsearch: Unknown time in date field

Laravel Migrations Date time field

Converting Date field and Character Time field to DateTime

Converting a date and and a char 'time' field to a single date

Convert custom date/time field to date only

Extract Time from Date-time field

pandas: create a new date time column by combining existing columns

What is the neat approach to combining the date and time value (both of datetime type)?

Combining java.util.Dates to create a date-time

ISO 8601 date-time format combining 'Z' and offset of '+0000'

Create a computed DataTime Column by combining Date and Time Columns

Combining different date and time columns to form a datetime value

Combining date and time columns into one column introduces NA values

Combining datetime and timezone columns to get a date time object